Search code examples
javahibernatedetachedcriteria

Java Hibernate: Passing current row id to Subquery


I have two Objects

Post {
   long id
   String post
   Date lastUpdate
}

Tracker {
   User user
   Post post
   Date viewed
}

User and Post are the composite ids for Tracker

While querying Post I want to figure out if the User has read the latest update on the Post

So the SQL should look something like this:

select count(*) as y0_
from nb_post AS this_ 
where (other_conditions) 
   or this_.lastUpdate > (select t_.viewed as y0_ from nb_post_tracker t_ where t_.post=this_.post)) 

My Criteria HAS to be on Post due to the other_conditions part. Is there a means to pass the current row's post.id through a DetachedCriteria or SubQuery?


Solution

  • As per Mark's suggestion I simply went and wrote a some plain old SQL query. Would be really great if there could be some mechanisms provided OOB in Hibernate for situations where Many-to-Many mapped entities are concerned and programmers want to join the two entities.