[英]How to make hibernate unidirectional many-to-many association updateable?
I have two entities - Category
and Attribute
. 我有两个实体 -
Category
和Attribute
。 Category
can have multiple related attributes, and Attribute
can be related to any number of categories. Category
可以具有多个相关属性, Attribute
可以与任意数量的类别相关。 Association should be available only on Category
side - Attribute
objects are not aware of categories they are related to. 关联应仅在
Category
侧可用 - Attribute
对象不知道它们与之相关的类别。
So I model this association as unidirectional many-to-many: 因此,我将此关联建模为单向多对多:
Category.hbm.xml Category.hbm.xml
<class name="Category" table="category" proxy="ICategory" entity-name="category">
<id name="id" column="id" unsaved-value="null"><generator class="identity" /></id>
...some properties...
<bag name="relatedAttributes" table="category_attribute" fetch="select">
<key column="id_category" />
<many-to-many column="id_attribute" entity-name="attribute" />
</bag>
</class>
and Attribute.hbm.xml 和Attribute.hbm.xml
<class name="Attribute" table="attribute" proxy="IAttribute" entity-name="attribute">
<id name="id" column="id" unsaved-value="null" ><generator class="identity" /></id>
...some properties...
</class>
Mapping works perfectly with current data until it needs an update. 映射与当前数据完美匹配,直到需要更新。 I just want to do things as simple as these:
我只想做一些简单的事情:
ICategory c = (ICategory) session.get("category", 1);
c.getRelatedAttributes().add((IAttribute) session.get("attribute", 2));
session.update("category", c);
How can i make this association updateable? 如何使这种关联可更新?
Finally done. 终于完成了。 Changes that affect behavior:
影响行为的变化:
<bag name="relatedAttributes" table="category_attribute" fetch="select" inverse="false" cascade="save-update">
...
</bag>
and do not forget to call session.flush()
after operations. 并且不要忘记在操作之后调用
session.flush()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.