1

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?

MandarK
  • 143
  • 1
  • 8
  • When things get complicated with Hibernate, you don't have to keep trying to figure out how to get Criteria to do what you want. You could instead use a direct SQL query(`Session.createSQLQuery()`). You can still add entities to the query so that it auto populates the results. – markbernard Jun 07 '16 at 18:32
  • Can I add just this part as the query? `this_.lastUpdate > (select t_.viewed as y0_ from nb_post_tracker t_ where t_.post=this_.post` – MandarK Jun 08 '16 at 03:52

1 Answers1

0

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.

MandarK
  • 143
  • 1
  • 8