休眠标准加入3个表

编程入门 行业动态 更新时间:2024-10-26 16:30:57
本文介绍了休眠标准加入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个表

本文发布于:2023-10-17 06:22:05,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标准

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!