Query in JPQL / HQL query

0

I need to query in HQL and I have this Hibernate entity.

 @Entity
 @Table(name = "TB_TIPO_DOCT", schema = "FEP")
 @NamedQueries({
 @NamedQuery(name = "TbTipoDoct.findAllDocumentosLiberados", query = 
 "SELECT t FROM TbTipoDoct t WHERE t.cdSituCnfr IS NULL ORDER BY 
 t.dsTipoDoct"),
 @NamedQuery(name = "TbTipoDoct.findAllDocumentosNaoLiberados", query = 
"SELECT t FROM TbTipoDoct t WHERE t.cdSituCnfr IS NOT NULL ORDER BY 
t.dsTipoDoct") })
public class TbTipoDoct implements Serializable {

    private static final long serialVersionUID = 1492325815547842716L;

    @Id
    @Basic(optional = false)
    @Column(name = "NR_SEQU_TIPO_DOCT")
    private Long nrSequTpDoct;
    @Column(name = "CD_DOCT")
    private Integer cdDoct;

What% of what I do is:

 SELECT * FROM FEP.TB_TIPO_DOCT WHERE NR_SEQU_TIPO_DOCT = 259;

My HQL query:

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();

Query query = session.createQuery("from TbTipoDoct tb where tb.nrSequTpDoct= 
:259");

Is it correct?

    
asked by anonymous 30.08.2018 / 21:25

1 answer

0

It's almost right.

For SQL:

 SELECT * FROM FEP.TB_TIPO_DOCT WHERE NR_SEQU_TIPO_DOCT = 259;

The HQL / JPQL equivalent, given its entity, would be:

Query query = session.createQuery("SELECT tb FROM TbTipoDoct tb WHERE tb.nrSequTpDoct = :numero");
query.setParameter("numero", 259L);

Notice that I made a correction using the value you want to fetch by adding a variable :numero and query.setParameter to set the value of the variable.

    
05.09.2018 / 22:24