Java 在 hibernate 中使用 inverse true
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20723013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using inverse true in hibernate
提问by Chaitanya
I am going through the hibernate documentation and came across the concept of inverse attribute. I am new to Hibernate so I am feeling difficulty in understanding the concept properly.
我正在阅读 hibernate 文档并遇到了逆属性的概念。我是 Hibernate 的新手,所以我很难正确理解这个概念。
<class name="Category">
<id name="id" column="CATEGORY_ID"/>
...
<bag name="items" table="CATEGORY_ITEM">
<key column="CATEGORY_ID"/>
<many-to-many class="Item" column="ITEM_ID"/>
</bag>
</class>
<class name="Item">
<id name="id" column="ITEM_ID"/>
...
<!-- inverse end -->
<bag name="categories" table="CATEGORY_ITEM" inverse="true">
<key column="ITEM_ID"/>
<many-to-many class="Category" column="CATEGORY_ID"/>
</bag>
</class>
From above code, the inverse="true"
is applied to categories, so I understood that categories is the inverse end.
从上面的代码中,inverse="true"
应用于类别,所以我理解类别是相反的一端。
But I am seeing some contradiction to my understanding:
但我发现与我的理解有些矛盾:
Changes made only to the inverse end of the association are not persisted.
仅对关联的反向端所做的更改不会持久化。
category.getItems().add(item); // The category now "knows" about the relationship
item.getCategories().add(category); // The item now "knows" about the relationship
session.persist(item); // The relationship won't be saved!
session.persist(category); // The relationship will be saved
If categories is on inverse end then how the relationship is saved here?
如果类别在相反的一端,那么这里如何保存关系?
The non-inverse side is used to save the in-memory representation to the database.
非逆端用于将内存中的表示保存到数据库中。
After looking at the example and reading above statement I came to know that categories is on non-inverse end.
在查看示例并阅读上述语句后,我开始知道类别处于非反向端。
Please help me in knowing how to interpret this inverse="true"
attribute. After searching in net and looking at answers in SO, I came to know the usefulness of this attribute but still I have this confusion.
请帮助我了解如何解释此inverse="true"
属性。在网上搜索并查看 SO 中的答案后,我开始知道这个属性的用处,但我仍然有这种困惑。
采纳答案by blackpanther
inverse="true"
basically means that the inverse relationship is also mapped within the class definition of the other class. But, it's real meaning is that it defines which side is the parent or the relationship owner for the two entities (parent or child). Hence, inverse="true"
in a Hibernate mapping shows that this class (the one with this XML definition) is the relationship owner; while the other class is the child.
inverse="true"
基本上意味着反向关系也映射到另一个类的类定义中。但是,它的真正含义是它定义了哪一方是两个实体(父或子)的父或关系所有者。因此,inverse="true"
在 Hibernate 映射中显示该类(具有此 XML 定义的类)是关系所有者;而另一个班级是孩子。
If you want to know more about this, then I would definitely have a look at this article: http://www.mkyong.com/hibernate/inverse-true-example-and-explanation/because it's easy to be misled of the meaning of this attribute in hibernate.
如果你想了解更多,那么我肯定会看看这篇文章:http: //www.mkyong.com/hibernate/inverse-true-example-and-explanation/因为很容易被误导这个属性在hibernate中的意义。
回答by Luis Teijon
Inversekeyword is created to defines which side is the owner to maintain the relationship. The procedure for updating and inserting varies according to this attribute.
创建反向关键字以定义维护关系的所有者是哪一方。更新和插入的过程因该属性而异。
Let's suppose we have two tables:
假设我们有两个表:
principal_table, middle_table
主体表,中间表
with a relationship of one to many. The hiberntate mapping classes are Principaland Middlerespectively.
一对多的关系。hibernate 映射类分别是Principal和Middle。
So the Principalclass has a SET of Middleobjects. The xml mapping file should be like following:
所以Principal类有一组Middle对象。xml 映射文件应如下所示:
<hibernate-mapping>
<class name="path.to.class.Principal" table="principal_table" ...>
...
<set name="middleObjects" table="middle_table" inverse="true" fetch="select">
<key>
<column name="PRINCIPAL_ID" not-null="true" />
</key>
<one-to-many class="path.to.class.Middel" />
</set>
...
As inverse is set to ”true”, it means “Middle” class is the relationship owner, so Principal class will NOT UPDATEthe relationship.
由于 inverse 设置为“true”,这意味着“Middle”类是关系所有者,因此 Principal 类不会更新关系。
So the procedure for updating could be implemented like this:
所以更新程序可以这样实现:
session.beginTransaction();
Principal principal = new Principal();
principal.setSomething("1");
principal.setSomethingElse("2");
Middle middleObject = new Middle();
middleObject.setSomething("1");
middleObject.setPrincipal(principal);
principal.getMiddleObjects().add(middleObject);
session.saveOrUpdate(principal);
session.saveOrUpdate(middleObject); // NOTICE: you will need to save it manually
session.getTransaction().commit();
Further information can be found HereIt is a well explained tutorial about how to use inverse attribute. It also shows how hinernate translate this into SQL queries.
可以在此处找到更多信息这是关于如何使用反向属性的详细解释教程。它还展示了 hinernate 如何将其转换为 SQL 查询。