简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM