我正在尝试在以下场景中使用 Criteria API:
- 我有两个表,
Schedule
并且Route
(带有它们的类和映射)。 Route
与 具有多对一关系Schedule
。Route
有一个整数属性sequence
。
现在我需要获取所有关联的 Route 对象满足以下条件的 Schedule 对象:
route.sequence=no. of all Route objects associated with the given Schedule object
我已经尝试了以下标准代码:
Criteria crit = getSession().createCriteria(getPersistentClass())
.createCriteria("routes", "route")
.setProjection(Projections.projectionList()
.add( Projections.rowCount(), "routeCount"))
.add(Restrictions.not(Restrictions.ltProperty("route.sequence", "routeCount")));
但它会生成以下 sql:
select count(*) as y0_
from schedule this_
inner join route route1_ on this_.ID=route1_.scheduleId
where route1_.sequence<y0_
并抛出以下错误:
Unknown column 'y0_' in 'where clause'
如果您有任何建议,请帮助我。