当我的应用程序启动时,JPA 给我一个错误。错误在命名查询中:
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)
实体代码:
@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...
然后在 DAO 中,我用这个方法调用它,即使在 EntityManager 的初始化时抛出异常(因为它是一个命名查询和东西)。但为了完整起见,这里是:
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();
}
回到命名查询...如果我删除AND r.cdPublico IN :publico
它可以工作,但我需要它。