Hibernate automatically appending 1=2 to the generated query
Hibernate is generating the SQL for the following query :
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))
as native PostgreSQL query :
select persistent0_.id as col_0_0_
from ems.nlt_content_profile persistent0_
where
1=2 and
persistent0_.service_id=? and
(exists (selec开发者_如何学Pythont 1
from ems.nlt_subscription subscripti1_
where subscripti1_.content_profile_id=persistent0_.id and
(subscripti1_.status in (?,?))))
Observed
'1=2'
getting appended to the query? Why is this getting added? Due to this, no records are retrieved.
This can happen for one of two reasons. It relates to polymorphic entities. To fix this, you need to make sure that you are passing the base class (not any subclass) to the createCriteria call. You also need to make sure that any subclasses represented in the database are registered with hibernate.
I figured out it was due to a configuration mistake. I was using @ForceDiscriminator and only the abstract class was configured in persistence.xml while configuration for any concrete subsclass was missing.
精彩评论