Hibernate的一對一,一對多/多對一關聯(lián)保存的實現(xiàn)
一對一關聯(lián)保存:
說明: 留言狀態(tài)表: 記錄用戶的留言信息,如留言人openid,留言時間等…(主表)
用戶留言內(nèi)容表: 記錄用戶的留言內(nèi)容,id與狀態(tài)表一對一(從表)
留言表實體類配置:(主表)
添加一對一關聯(lián)的從表
// one to one private com.rhtcms.cms.entity.main.CustomerMessageContent customerMessageContent;//對應內(nèi)容表信息
留言內(nèi)容表配置:(從表)
添加一對一關聯(lián)的主表:
//one to one private com.rhtcms.cms.entity.main.CustomerMessage customerMessage;//對應客戶留言狀態(tài)表
留言表hbm.xml配置:(主表)
1.主表id采用uuid隨機生成
2.設置one-to-one 的從表
<id name="id" type="string" column="id"><generator class = "uuid"/></id> <one-to-one name="customerMessageContent" class="CustomerMessageContent" cascade="all"></one-to-one>
留言內(nèi)容表hbm.xml配置:(從表)
1.主鍵生成策略使用:foreign 使用另外一個相關聯(lián)的對象的主鍵作為該對象主鍵。主要用于一對一關系中。
2.設置one-to-one 的主表
<id name="messageId" type="string" column="message_id">
<generator class="foreign"><param name="property">customerMessage</param></generator>
</id>
<one-to-one name="customerMessage" class="CustomerMessage" constrained="true" ></one-to-one>
保存方法:
思路:1.保存時先set主表數(shù)據(jù),先將主表保存
2: set從表數(shù)據(jù),其中關鍵一步,從表須set剛剛保存的主表,然后再保存從表
CustomerMessage customerMessageSave = mng.save(customerMessage); //保存狀態(tài)表信息,并獲取uuid
CustomerMessageContent customerMessageContent = new CustomerMessageContent();
//首先對留言,反饋信息進行敏感詞過濾
String wordCensor = cmsMng.replaceSensitivity(questionText);
customerMessageContent.setQuestionText(wordCensor);//設置內(nèi)容表內(nèi)容
customerMessageContent.setCustomerMessage(customerMessageSave);//設置從表中主表的屬性
contentMng.save(customerMessageContent);//同時保存內(nèi)容表信息
一對多/多對一保存
說明:1.活動表:保存活動信息,記錄活動報名的信息,如場景,主辦方,報名時間…(主表)
2.自定義報名信息表,因為報名要素不確定性,eg: 報名要素有 ,電話,姓名,門票id依據(jù)…(從表)
活動表實體類配置:(主表)
1.添加自定義報名信息從表
private Set<ActivitySign> signs = new HashSet<ActivitySign>();
自定義報名要素實體類配置:(從表)
1.添加關聯(lián)的活動表實體類
private Activity activity;//活動表信息
活動表hbm.xml配置:(主表)
1.主鍵采用 "native"策略(hilo+identity+sequence三選一)
ps:根據(jù)底層數(shù)據(jù)庫對自動生成標識符的能力來選擇i dentity、sequence、hilo三種生成器中的一種,適合跨數(shù)據(jù)庫平臺開發(fā)
2.配置一對多的報名信息從表
<id name="id" type="java.lang.Integer" column="activity_id"><generator class="native"/></id> <set name="signs" cascade="save-update" lazy="false"> <cache usage="read-write"/> <key column="activity_id" /> <one-to-many class="ActivitySign"/> </set>
報名信息表:(從表)
配置多對一: name:主表多對一的屬性 ,class: 主表實體類 column: 多對一字段
<id name="id" column="sign_id"> <generator class="native"></generator> </id> <many-to-one name="activity" class="Activity" column="activity_id"></many-to-one>
保存方法:
保存思路
1:保存方法帶兩個參數(shù): 參數(shù)1: 主表的數(shù)據(jù) ;參數(shù)2: 從表的數(shù)據(jù)
2.實現(xiàn)層首先保存主表,然后再遍歷從表,還是一樣的,從表先設置主表,然后再保存從表
activityMng.save(bean,set);
//......下面是save方法
bean = dao.save(bean);
// update by 2021年1月23日10:36:56 zqy 優(yōu)化保存接口
for (ActivitySign activitySign : set) {
activitySign.setActivity(bean);
signDao.save(activitySign);
}
到此這篇關于Hibernate的一對一,一對多/多對一關聯(lián)保存的實現(xiàn)的文章就介紹到這了,更多相關Hibernate一對一,一對多/多對一內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot-Starter造輪子之自動鎖組件lock-starter實現(xiàn)
這篇文章主要為大家介紹了Springboot-Starter造輪子之自動鎖組件lock-starter實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
詳解json string轉換為java bean及實例代碼
這篇文章主要介紹了詳解json string轉換為java bean及實例代碼的相關資料,這里提供實例代碼幫助大家理解,需要的朋友可以參考下2017-07-07
如何解決executors線程池創(chuàng)建的線程不釋放的問題
這篇文章主要介紹了如何解決executors線程池創(chuàng)建的線程不釋放的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
使用SpringBoot + Redis + Vue實現(xiàn)動態(tài)路由加載頁面的示例代
在現(xiàn)代 Web 應用開發(fā)中,動態(tài)路由加載能夠顯著提升應用的靈活性和安全性,本文將深入探討如何利用 Spring Boot、Redis、Element UI 和 Vue 技術棧實現(xiàn)動態(tài)路由加載,并通過 Redis 生成和驗證有效鏈接以實現(xiàn)頁面訪問控制,需要的朋友可以參考下2024-09-09
SpringBoot數(shù)據(jù)訪問的實現(xiàn)
本文主要介紹了SpringBoot數(shù)據(jù)訪問的實現(xiàn),引入各種xxxTemplate,xxxRepository來簡化我們對數(shù)據(jù)訪問層的操作,感興趣的可以了解一下2023-11-11

