命名查询中的异常

时间:2012-12-14 15:50:21

标签: jpa

当我的应用程序启动时,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的init处抛出的(因为它是一个命名查询和东西)。但为了完整起见,这里有:

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它可以工作,但我需要它。

2 个答案:

答案 0 :(得分:1)

像这样修改查询

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

在查询中使用IN statemet时,它应该进入()。

答案 1 :(得分:0)

解释TbPublico如何成为List<Integer> ???它可能是一个List<TbPublico>