在hibernate中使用inverse true [英] Using inverse true in hibernate

查看:75
本文介绍了在hibernate中使用inverse true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览hibernate文档,并且遇到了inverse属性的概念。我是Hibernate的新手,所以我很难理解这个概念。

http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/collections.html#collections-bidirectional

 < 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定义的类)是关系所有者;而另一个类是孩子。



如果你想知道更多关于这个,那么我肯定会看看这篇文章: http://www.mkyong.com/hibernate/inverse-true-example-and-explanation/ ,因为在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.

http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/collections.html#collections-bidirectional

<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:

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" 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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