休眠标准加入3个表 [英] Hibernate Criteria Join with 3 Tables

查看:97
本文介绍了休眠标准加入3个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Dokument.class
被映射到角色
roleId



Role.class
有一个ContactPerson
contactId



Contact.class
名字
LastName



我想在Contact类中搜索First或LastName并检索已连接的Dokuments列表。

  session.createCriteria(Dokument.class)
.setFetchMode(角色,FetchMode.JOIN)
.setFetchMode(contact,FetchMode.JOIN)
.add(Restrictions.eq(LastName,Test))。list();

我得到一个错误无法解析类Dokument的属性姓氏



有人可以解释为什么连接在Dokument上搜索,而不是在所有连接的表上搜索?提前感谢所有帮助!

解决方案

获取模式仅表示必须获取关联。如果你想添加关联实体的限制,你必须创建一个别名或者一个子标准。我通常更喜欢使用别名,但是YMMV:

  Criteria c = session.createCriteria(Dokument.class,dokument); 
c.createAlias(dokument.role,role);默认情况下为内部连接
c.createAlias(role.contact,contact);
c.add(Restrictions.eq(contact.lastName,Test));
返回c.list();

这当然在 Hibernate参考手册,以及 javadoc for Criteria 甚至有例子。阅读文档:它有很多有用的信息。


I am looking for a hibernate criteria to get following:

Dokument.class is mapped to Role roleId

Role.class has a ContactPerson contactId

Contact.class FirstName LastName

I want to search for First or LastName on the Contact class and retrieve a list of Dokuments connected.

I have tried something like this:

session.createCriteria(Dokument.class)
.setFetchMode("role",FetchMode.JOIN)
.setFetchMode("contact",FetchMode.JOIN)
.add(Restrictions.eq("LastName","Test")).list();

I get an error could not resolve property "LastName" for class "Dokument"

Can someone explain why the join searches on Dokument and not on all joined tables? Thanks in advance for all the help!

解决方案

The fetch mode only says that the association must be fetched. If you want to add restrictions on an associated entity, you must create an alias, or a subcriteria. I generally prefer using aliases, but YMMV:

Criteria c = session.createCriteria(Dokument.class, "dokument");
c.createAlias("dokument.role", "role"); // inner join by default
c.createAlias("role.contact", "contact");
c.add(Restrictions.eq("contact.lastName", "Test"));
return c.list();

This is of course well explained in the Hibernate reference manual, and the javadoc for Criteria even has examples. Read the documentation: it has plenty of useful information.

这篇关于休眠标准加入3个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆