I have two entities - Category
and Attribute
. Category
can have multiple related attributes, and Attribute
can be related to any number of categories. Association should be available only on Category
side - Attribute
objects are not aware of categories they are related to.
So I model this association as unidirectional many-to-many:
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
<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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.