Hibernate 标准 API
我需要进行搜索,其中涉及向下导航到子元素,然后返回到根/父元素。我如何使用 Criteria API 来做这样的事情?
作为一个例子,我有:
Criteria crit = getSessionFactory().getCurrentSession()
.createCriteria(TherapistImpl.class);
现在我需要转到 User 类来获取与用户关联的地址(一组地址):
crit.createCriteria("user")
.createCriteria("addresses")
.add(Restrictions.eq("postcode", postCode));
但是现在我已经完成了此操作,我需要返回以获取其他地址来自父类 TherapyImpl 的一对多关联,例如
crit.createCriteria("therapyProvisions")
.add(Restrictions.eq("type", searchByValueSelected));
,但 hibernate 认为这是指我们之前链接和创建的用户标准。
那么我要创建多个标准吗?有没有办法导航回树等?
感谢您的评论 保罗
I need to do a search which involves navigating down to the child elements and then back up to the root/parent. How do I do such a thing using the Criteria API?
As an example I have:
Criteria crit = getSessionFactory().getCurrentSession()
.createCriteria(TherapistImpl.class);
Now I need to go to the User class to get the address which is associated with the user ( a set of Addresses):
crit.createCriteria("user")
.createCriteria("addresses")
.add(Restrictions.eq("postcode", postCode));
But now I've done this, I need to go back up to get the other one-to-many associations from the parent class TherapyImpl e.g.
crit.createCriteria("therapyProvisions")
.add(Restrictions.eq("type", searchByValueSelected));
But hibernate thinks this is referring to the User criteria that we chained and created earlier.
So do I create multiple criterias?? Is there a way of navigating back up the tree etc?
thanks for your comments
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要为
user
关联创建子Criteria
,而是为其创建一个别名:(您可能需要为
addresses
创建一个子别名>.)然后,对therapyProvisions 执行相同的操作:
现在您将拥有内部连接。
Rather than creating a sub-
Criteria
for theuser
association, create an alias for it:(You might need to create a sub-alias for
addresses
.)Then, do the same for
therapyProvisions
:Now you'll have inner joins all around.