Hibernate会自动将1 = 2附加到生成的查询中

时间:2011-08-04 11:40:38

标签: hibernate jpa jpql

Hibernate正在为以下查询生成SQL:

select pcp.id from PersistentContentProfile pcp where pcp.service.id = :service_id and exists(select 1 from Subscription s where s.contentProfile.id = pcp.id and s.status in (:statuses))

作为本机PostgreSQL查询:

select persistent0_.id as col_0_0_ 
from ems.nlt_content_profile persistent0_ 
where 
1=2 and 
persistent0_.service_id=? and 
(exists (select 1 
        from ems.nlt_subscription subscripti1_ 
        where subscripti1_.content_profile_id=persistent0_.id and 
        (subscripti1_.status in (?,?))))

观察

  

'1 = 2'

附加到查询?这为什么会被添加?因此,不会检索任何记录。

2 个答案:

答案 0 :(得分:-1)

这可能由于以下两个原因之一而发生。它涉及多态实体。要解决此问题,您需要确保将基类(不是任何子类)传递给createCriteria调用。您还需要确保数据库中表示的任何子类都使用hibernate注册。

答案 1 :(得分:-1)

我发现这是由于配置错误造成的。我正在使用@ForceDiscriminator,并且只在persistence.xml中配置了抽象类,而缺少任何具体子类的配置。