【发布时间】:2016-06-07 18:25:53
【问题描述】:
我有两个对象
Post {
long id
String post
Date lastUpdate
}
Tracker {
User user
Post post
Date viewed
}
User 和 Post 是 Tracker 的复合 ID
在查询Post时,我想知道User是否已经阅读了Post上的最新更新
所以 SQL 应该是这样的:
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))
由于 other_conditions 部分,我的 Criteria 必须在 Post 上。有没有办法通过DetachedCriteria 或SubQuery 传递当前行的post.id?
【问题讨论】:
-
当 Hibernate 变得复杂时,您不必一直试图弄清楚如何让 Criteria 做您想做的事情。您可以改为使用直接 SQL 查询 (
Session.createSQLQuery())。您仍然可以将实体添加到查询中,以便它自动填充结果。 -
我可以只添加这部分作为查询吗?
this_.lastUpdate > (select t_.viewed as y0_ from nb_post_tracker t_ where t_.post=this_.post
标签: java hibernate detachedcriteria