在 HQL 中,我可以执行以下操作:
select roleHeldByOwner.TargetPerson
from Person roleOwner
join roleOwner.RolesOnPeople roleHeldByOwner
where roleOwner.Id = :roleOwnerId
如何在 Criteria 查询中实现相同的目标?专门选择不是from
子句中第一个实体的东西。
在 HQL 中,我可以执行以下操作:
select roleHeldByOwner.TargetPerson
from Person roleOwner
join roleOwner.RolesOnPeople roleHeldByOwner
where roleOwner.Id = :roleOwnerId
如何在 Criteria 查询中实现相同的目标?专门选择不是from
子句中第一个实体的东西。
您可以通过创建子标准来加入,并使用 Projections 选择标量结果。您的条件查询可能类似于:
session.CreateCriteria(typeof(Person))
.Add(Restrictions.Eq("Id", roleOwnerId))
.SetProjection(Projections.Property("TargetPerson"))
.CreateCriteria("RolesOnPeople", JoinType.InnerJoin) // Or LeftOuterJoin, etc.
.List();
如果您需要多个投影,请使用 ProjectionList,例如:
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("...", ...))
.Add(...)
...
)
我不确定您的域是什么样的,或者您试图从查询中得到什么,因此上述内容可能不完全正确。但是,这应该是您开始所需要的。