簡體   English   中英

休眠一對多關系以將數據保存到多個表

[英]Hibernate one to many relationship for saving data to multiple tables

我有以下兩個處於休眠狀態的表:一對多關系。

1)通知

<class name="Notification" table="notifications" > <id name="id" column="id"><generator class="native"/></id>

<set name="Preference" table="preference" cascade="all" inverse="true" lazy="false" fetch="select"> <key> <column name="user_id" not-null="true" /> </key>

<one-to-many class="Preference" /> </set> </class>


(注意:在Notification類中,我有一個像
Set<Preference> Preference


2)偏好

<class name="Preference" table="preference"> <id name="id" column="id"> <generator class="native"/> </id>

<property name="userId" column="user_id" />

<property name="sectorId" column="sector_id" />

<property name="stateId" column="state_id" /> </class>

我用過的Hiberante 查詢

通知通知= ......; (將對象插入到此Notification對象中)

getHibernateTemplate()。merge(notification);

因此,問題是未保存“首選項”的對象。 但通知數據已正確保存。

提前致謝!

我已經找到了答案。 答案是將(一對多)休眠配置inverse =“ true”中的屬性更改為false 如果將inverse設置為true ,則必須配置另一類中的另一對多配置。

如果我在上面提出的問題是invese =“ true”則要進行更改。

2)偏好

<class name="Preference" table="preference"> <id name="id" column="id"> 
<generator class="native"/> </id>
......
<many-to-one name="notification" class="Notification" fetch="select">
   <column name="id" not-null="true" />
</many-to-one>
</class>

多謝你們!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM