Doubt in JPQL/HQL query

Asked

Viewed 116 times

0

I need to make an appointment in HQL and I have this entity Hibernate.

 @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;

The select 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");

She is correct?

  • Please try to elaborate titles that evoke your real doubt. Texts such as "doubt in query" is too generic and does not demonstrate the content of your question.

  • Did your query work? tested it?

  • Try running your select and if errors occur, post here.

1 answer

0

She is almost right.

To the SQL:

 SELECT * FROM FEP.TB_TIPO_DOCT WHERE NR_SEQU_TIPO_DOCT = 259;

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

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

Note that I made a correction in using the value you want to search for, adding a variable :numero and the query.setParameter to set the variable value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.