0

JPA is giving me an error when my application goes up. The error is in the named query:

org.hibernate.HibernateException: Errors in named queries: MyEntity.MyQuery org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:435)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:883)
org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)

The Entity code:

@NamedQuery(name = "MyEntity.MyQuery", query = "SELECT r FROM MyEntity r WHERE r.cdSecao = :secao AND r.cdPublico IN :publico")


// other class stuff declarations...


    @JoinColumn(name = "cd_publico", referencedColumnName = "cd_publico")
@ManyToOne
private TbPublico cdPublico;

@JoinColumn(name = "cd_secao", referencedColumnName = "cd_secao")
@ManyToOne
private TbSecao cdSecao;

// getters setters and assorted shenanigans...

Then in the DAO, I invoke it with this method, even though the exception is being thrown at the init of the EntityManager (because its a named query and stuff). But for the sake of completeness, here it goes:

public List<MyEntity> listMyEntity(String idPublico, TbSecao secao) {
    List<TbPublico> listaPublico = OtherClass.doMcGuffin(idPublico);

    EntityManager em = EMFactory.getEntityManager();
    TypedQuery<MyEntity> theQuery= em.createNamedQuery("MyEntity.MyQuery", MyEntity.class);
    theQuery.setParameter("secao", secao);
    theQuery.setParameter("publico", listaPublico);

    return theQuery.getResultList();

}   

Back to the named query... if I remove the AND r.cdPublico IN :publico it works, but I need it.

2 Answers 2

1

Modify the query like this

SELECT r FROM MyEntity r WHERE r.cdSecao = :secao AND r.cdPublico IN (:publico)

When you use the IN statemet in your query then it should come into the ().

Sign up to request clarification or add additional context in comments.

Comments

0

Explain how a TbPublico can be IN a List<Integer> ??? It could possibly be IN a List<TbPublico>

4 Comments

MAYBE A TYPO. I had to change some stuff in order to obfuscate my real source code (company policy) so I could post it on S.O. without my boss yanking my ears.
Also, this is not a real answer, you should use a comment for stuff like that, i think ;()
If you have a typo I suggest that you correct your question, because obviously as it is currently my answer points out an error in it and consequently a possible reason why your JPA provider throws an exception.
The JPA framework loads the named queries annotated in entities at server startup, by the time the DAO code is executed, the hybernate exception has been long thrown. Actually, it is not even executed, because the entity manager never loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.