hibernate07--关联映射
生活随笔
收集整理的這篇文章主要介紹了
hibernate07--关联映射
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
單向的一對多關聯
創建對應的實體類以及映射文件
package cn.bdqn.bean; /*** * @author 小豆腐*街道對應的實體類**單向的多對一關聯*/ public class Street {private Integer id;private String name;//多個街道 屬于 一個區縣private District district; //對應的區縣public District getDistrict() {return district;}public void setDistrict(District district) {this.district = district;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Street(Integer id, String name) {super();this.id = id;this.name = name;}public Street() {super();}@Overridepublic String toString() {return "Street [id=" + id + ", name=" + name + "]";}}?
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.bdqn.bean"><class name="Street"><id name="id"> <generator class="assigned"/><!-- 手動給主鍵賦值 --></id> <property name="name"/><!-- 配置多對一關聯name:對應的是 本類中 關聯關系的屬性名column:對應數據庫中 兩個表的 外鍵!class:關聯的實體類--><many-to-one name="district" column="districtId" class="District"/> </class> </hibernate-mapping>?
package cn.bdqn.bean; /*** @author 小豆腐**區縣的實體類*/ public class District {private Integer id;private String name;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public District(Integer id, String name) {super();this.id = id;this.name = name;}public District() {super();}@Overridepublic String toString() {return "Street [id=" + id + ", name=" + name + "]";}}?
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.bdqn.bean"><class name="District"><id name="id"> <generator class="assigned"/><!-- 手動給主鍵賦值 --></id> <property name="name"/></class> </hibernate-mapping>?
需要在hibernate.cfg.xml文件中 配置映射文件
?
?
測試類
Session session=null;Transaction transaction=null;//獲取session 開啟事務 @Beforepublic void before(){session=HibernateSessionUtil.getCurrentSession();transaction= session.beginTransaction();}/*** 測試單向的多對一的關聯* * 創建對應的區縣 新增幾個區縣 以備后續使用*/@Testpublic void testAdd(){District district=new District(3, "區縣3");session.save(district); //保存transaction.commit(); //提交事務 }//新增 街道的同時 給街道對應的區縣賦值 @Testpublic void test01(){//創建一個街道Street street=new Street();street.setId(1);street.setName("街道1");//給對應的區縣賦值District district= (District) session.load(District.class, 1);street.setDistrict(district);session.save(street); //保存transaction.commit(); //提交事務 只會產生一條sql insert }//修改 街道對應的區縣 @Testpublic void test02(){//從數據庫中獲取一個街道Street street=(Street) session.load(Street.class, 1); // 對應的區縣是1District district=(District) session.load(District.class, 2); // 區縣是2//修改street.setDistrict(district); // 不需要 update 因為兩個對象都是持久化對象//提交事務 transaction.commit();}//刪除 街道對應的區縣 @Testpublic void test03(){//從數據庫中獲取一個街道Street street=(Street) session.load(Street.class, 1); // 對應的區縣是2//修改street.setDistrict(null); //提交事務 transaction.commit();}?
?
?
雙向的一對多關聯就是在單向多對一的基礎上增加一個單向的一對多!
修改District代碼 一對多關聯
package cn.bdqn.bean;import java.util.ArrayList; import java.util.List;/*** @author 小豆腐**區縣的實體類* 一對多的關聯關系*/ public class District {private Integer id;private String name;// 一個區縣 有 多個街道private List<Street> streets=new ArrayList<>();public List<Street> getStreets() {return streets;}public void setStreets(List<Street> streets) {this.streets = streets;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public District(Integer id, String name) {super();this.id = id;this.name = name;}public District() {super();}@Overridepublic String toString() {//如果寫成streets 會出現 堆棧溢出的異常!return "District [id=" + id + ", name=" + name + ", streets=" + streets.size()+ "]";}}?
修改District.hbm.xml文件代碼
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.bdqn.bean"><class name="District"><id name="id"> <generator class="assigned"/><!-- 手動給主鍵賦值 --></id> <property name="name"/><!-- 設置一對多 name:本類中的關聯屬性名 集合的名稱column: 就是數據庫表中的外鍵order-by="id desc" 按照 街道的id 進行 降序排列inverse:是由誰來維護表與表之間的關系! 默認是false!(維護) true(不維護)--><bag name="streets" cascade="all" inverse="true"><key column="districtId"/><one-to-many class="Street"/></bag></class> </hibernate-mapping>?
在測試類中增加代碼
?
/*** 創建幾個街道 測試 雙向的一對多關聯*/@Testpublic void test04(){Street street=new Street();street.setId(4);street.setName("街道4");//給對應的區縣賦值District district= (District) session.load(District.class, 1);street.setDistrict(district);session.save(street); //保存//提交事務 transaction.commit();}//根據區縣 獲取所管轄的街道 @Testpublic void test05(){District district=(District) session.load(District.class, 1);List<Street> streets = district.getStreets();for (Street street : streets) {System.out.println(street);}}/*** 根據區縣 獲取所管轄的街道 根據街道的編號 降序排列* 01.只需要在 bag節點中 增加 order-by屬性 = id desc*/@Testpublic void test06(){District district=(District) session.load(District.class, 1);List<Street> streets = district.getStreets();for (Street street : streets) {System.out.println(street);}}/*** 02.使用hql語句*/@Testpublic void test07(){String hql="from Street s where districtId=:id order by s.id desc";//創建query對象Query query = session.createQuery(hql);//給參數賦值query.setParameter("id", 1); //區縣編號是1//遍歷結果集List<Street> list = query.list();for (Street street : list) {System.out.println(street);}}/*** cascade屬性:定義的是關系兩端 對象到對象的級聯關系!* 必須是 雙向的一對多關聯!* * 常用的屬性值:* 01.none:默認值!當session操作當前對象的時候,忽略關聯的屬性!* 02.save-update:當session調用save,saveorUpdate以及update()的時候!* 會級聯的保存和修改當前對象以及對象關聯的屬性!* 001.去區縣的xml文件中 的 bag節點 增加 cascade屬性! * 002.直接運行以下代碼 * 03.delete: 當session調用 delete()的時候,會級聯刪除所關聯的對象 !* 04.all: 包括了save-update和delete!* * 添加區縣的同時 添加街道*/@Testpublic void test08(){//創建區縣District district=new District(4, "區縣4");//創建街道Street street1=new Street(5, "街道5");Street street2=new Street(6, "街道6");Street street3=new Street(7, "街道7");//給區縣的街道賦值 district.getStreets().add(street1);district.getStreets().add(street2);district.getStreets().add(street3);//保存區縣session.save(district); //發現 保存了 區縣 但是 沒有保存對應的街道! transaction.commit();/*** 程序執行之后的結果:* Hibernate: insert into District (name, id) values (?, ?)Hibernate: update Street set districtId=? where id=?Hibernate: update Street set districtId=? where id=?Hibernate: update Street set districtId=? where id=?*//*** 在xml文件中設置了 cascade="save-update" 之后的結果* * 問題:雖然級聯保存成功了!* 但是有多余update語句!* 為什么會出現多余的? 兩個對象都在維護彼此之間的關系!* * Hibernate: select street_.id, street_.name as name1_, street_.districtId as districtId1_ from Street street_ where street_.id=?Hibernate: select street_.id, street_.name as name1_, street_.districtId as districtId1_ from Street street_ where street_.id=?Hibernate: select street_.id, street_.name as name1_, street_.districtId as districtId1_ from Street street_ where street_.id=?Hibernate: insert into District (name, id) values (?, ?)Hibernate: insert into Street (name, districtId, id) values (?, ?, ?)Hibernate: insert into Street (name, districtId, id) values (?, ?, ?)Hibernate: insert into Street (name, districtId, id) values (?, ?, ?)Hibernate: update Street set districtId=? where id=?Hibernate: update Street set districtId=? where id=?Hibernate: update Street set districtId=? where id=?*/}/*** cascade="delete" xml中的配置* 刪除 區縣 的同時 刪除 街道!*/@Testpublic void test09(){District district= (District) session.load(District.class, 1);//獲取三個街道Street street1= (Street) session.load(Street.class, 2);Street street2= (Street) session.load(Street.class, 3);Street street3= (Street) session.load(Street.class, 4);session.delete(district);transaction.commit();/*** Hibernate: update Street set districtId=null where districtId=?Hibernate: delete from Street where id=?Hibernate: delete from Street where id=?Hibernate: delete from Street where id=?Hibernate: delete from District where id=?*/}/*** 針對于 多余的sql語句 解決辦法!* * 只需要一方來維護表之間的關系!* inverse屬性:* 01.默認是false(由我維護!)* 02.inverse=true:這一方不維護關系!(不與數據庫交互)* * * 不能都維護,也不能都 不維護! 這個時候關鍵是誰來維護這個關系?* 雙向的一對多! * hibernate:規定多的一端來維護關系,那么必須在一的一方設置 inverse=true:*/@Testpublic void test10(){//創建區縣District district=new District(4, "區縣4");//創建街道Street street1=new Street(5, "街道5");Street street2=new Street(6, "街道6");Street street3=new Street(7, "街道7");//給區縣的街道賦值 district.getStreets().add(street1);district.getStreets().add(street2);district.getStreets().add(street3);//保存區縣session.save(district); //發現 保存了 區縣 但是 沒有保存對應的街道! transaction.commit();/*** * 發現 沒有了 update語句!* Hibernate: select street_.id, street_.name as name1_, street_.districtId as districtId1_ from Street street_ where street_.id=?Hibernate: select street_.id, street_.name as name1_, street_.districtId as districtId1_ from Street street_ where street_.id=?Hibernate: select street_.id, street_.name as name1_, street_.districtId as districtId1_ from Street street_ where street_.id=?Hibernate: insert into District (name, id) values (?, ?)Hibernate: insert into Street (name, districtId, id) values (?, ?, ?)Hibernate: insert into Street (name, districtId, id) values (?, ?, ?)Hibernate: insert into Street (name, districtId, id) values (?, ?, ?)*/}?
轉載于:https://www.cnblogs.com/999-/p/6404243.html
總結
以上是生活随笔為你收集整理的hibernate07--关联映射的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: e行卡粤通卡是一样的吗
- 下一篇: e闪贷秒拒原因