0

当我的应用程序启动时,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它可以工作,但我需要它。

4

2 回答 2

1

像这样修改查询

从 MyEntity r 中选择 r,其中 r.cdSecao = :secao AND r.cdPublico IN (:publico)

当您在查询中使用 IN 语句时,它应该进入 ()。

于 2012-12-16T03:38:43.777 回答
0

解释 a 怎么TbPublico可能是 IN a List<Integer>??? 它可能是在一个List<TbPublico>

于 2012-12-15T10:37:39.893 回答