2015-03-02 40 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()