Search code examples
javahibernatehibernate-criteria

How initialize a LazyCollection with Join Fetch


ProdutoValor it's a table which contains a many to one relationship with Produto. This is how I get all data in ProdutoValor:

    @Override
    public List<ProdutoValorETO> getAll() {
        String query = " SELECT * FROM produtovalor WHERE ativo = TRUE; ";
        SQLQuery eQuery = getCurrentSession().createSQLQuery(query).addEntity(ProdutoValorETO.class);
        return CastUtils.castList(eQuery.list(), ProdutoValorETO.class);
    }

But after this I have a nullPointer if try get Produto of any element of this list. E.g: pv.getProduto().getCodigo() (Note that pv is an element of this list and produto is null)

I read that JOIN FETCH can initialize Produto in just one query. So I tried:

    @Override
    public List<ProdutoValorETO> getAll() {
        Query query =
                getCurrentSession().createQuery("SELECT e FROM produtovalor e JOIN FETCH e.produto");
        return query.list();
    }

And now I get a querySyntaxException:

Caused by: org.hibernate.hql.ast.QuerySyntaxException: produtovalor is not mapped [SELECT e FROM produtovalor e JOIN FETCH e.produto]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
    at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
    at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:71)
    at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:295)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3228)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3112)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:571)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:288)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:231)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:162)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:113)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1624)

Class:

@Entity
@Table(name = "produtoValor")
public class ProdutoValorETO extends BaseTO {

    @Id
    @Column(name = "id", nullable = false)
    @SequenceGenerator(name = "id", sequenceName = "produto_valor_sequence", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id")
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "idLojista")
    private LojistaTO lojista;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "idProduto")
    private ProdutoETO produto;

    @Column
    private Integer quantidadeMinima;

    @Column
    private Integer quantidadeMaxima;

    @Column
    private Double valor;

    @Column
    private Integer estoque;

    @Column
    private Integer estoqueComprometido;

    //getters and setters
}

How can I get a list of ProdutoValor with Produto?


Solution

  • The error is about hibernate complaining that he can't find the "produtovalor" entity.

    In Hql queries you have to use the entity name not the table name

    ("SELECT e FROM produtovalor e JOIN FETCH e.produto")
    

    should be

    ("SELECT e FROM ProdutoValorETO e JOIN FETCH e.produto")
    

    Also you should avoid using sql native query unless necesssary as it reduce the portability of your app to different datasource