问题描述
我正在浏览hibernate文档,并且遇到了inverse属性的概念。我是Hibernate的新手,所以我很难理解这个概念。
< class name =Category>
< id name =idcolumn =CATEGORY_ID/>
...
< bag name =itemstable =CATEGORY_ITEM>
< key column =CATEGORY_ID/>
< / bag>
< / class>
< class name =Item>
< id name =idcolumn =ITEM_ID/>
...
< bag name =categoriestable =CATEGORY_ITEMinverse =true>
< key column =ITEM_ID/>
< / bag>
< / class>
从上面的代码中, inverse =true
适用于类别,所以我明白类别是相反的结果。
但我看到一些与我理解的矛盾:
category.getItems()。add(item); //现在类别知道关系
item.getCategories()。add(category); //该项目现在知道关系
session.persist(item); //关系不会被保存!
session.persist(category); //关系将被保存
如果类别在反向结束,那么关系如何保存在这里?
查看示例并阅读上述语句后,我开始知道类别处于非反转状态。
请帮助我了解如何解释 inverse =true
属性。在网上搜索并在SO中查看答案之后,我开始了解这个属性的用处,但我仍然有这种困惑。
inverse =true
基本上意味着反向关系也映射到另一个类的类定义中。但是,它的真正含义是它定义了哪一方是父母或两个实体(父母或子女)的关系所有者。因此,在Hibernate映射中, inverse =true
表明这个类(具有这个XML定义的类)是关系所有者;而另一个类是孩子。
如果你想知道更多关于这个,那么我肯定会看看这篇文章:,因为在hibernate中很容易误导这个属性的含义。
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.
<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.
But I am seeing some contradiction to my understanding:
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?
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"
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.
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.
这篇关于在hibernate中使用inverse true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!