2015-03-02 65 views
0

我有两个实体 - CategoryAttributeCategory可以有多个相关属性,并且Attribute可以与任意数量的类别相关。协会应仅在Category一侧提供 - Attribute对象不知道它们与之相关的类别。如何让hibernate单向多对多关联更新?

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> 

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

测绘工程完美搭配:

所以我这个协会为单向多到多模当前数据,直到它需要更新。我只想做的事情一样简单,因为这些:

ICategory c = (ICategory) session.get("category", 1); 
c.getRelatedAttributes().add((IAttribute) session.get("attribute", 2)); 
session.update("category", c); 

我怎样才能使这种关联更新?

回答

0

最后完成。影响行为的变化:

<bag name="relatedAttributes" table="category_attribute" fetch="select" inverse="false" cascade="save-update"> 
    ... 
</bag> 

并且不要忘记在操作后致电session.flush()