兩個實體類:客戶與聯繫人,一個客戶可以有多個聯繫人 客戶類: package domain; import java.util.HashSet; import java.util.Set; //客戶實體 public class Customer { private Long cust_id; pr ...
兩個實體類:客戶與聯繫人,一個客戶可以有多個聯繫人
客戶類:
package domain; import java.util.HashSet; import java.util.Set; //客戶實體 public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; //使用set集合,表達一對多關係 private Set<LinkMan> linkMens = new HashSet<LinkMan>(); public Set<LinkMan> getLinkMens() { return linkMens; } public void setLinkMens(Set<LinkMan> linkMens) { this.linkMens = linkMens; } public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } @Override public String toString() { return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]"; } }View Code
聯繫人類:
package domain; //聯繫人實體 public class LinkMan { private Long lkm_id; private Character lkm_gender; private String lkm_name; private String lkm_phone; private String lkm_email; private String lkm_qq; private String lkm_mobile; private String lkm_memo; private String lkm_position; // 表達多對一關係 private Customer customer; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public Long getLkm_id() { return lkm_id; } public void setLkm_id(Long lkm_id) { this.lkm_id = lkm_id; } public Character getLkm_gender() { return lkm_gender; } public void setLkm_gender(Character lkm_gender) { this.lkm_gender = lkm_gender; } public String getLkm_name() { return lkm_name; } public void setLkm_name(String lkm_name) { this.lkm_name = lkm_name; } public String getLkm_phone() { return lkm_phone; } public void setLkm_phone(String lkm_phone) { this.lkm_phone = lkm_phone; } public String getLkm_email() { return lkm_email; } public void setLkm_email(String lkm_email) { this.lkm_email = lkm_email; } public String getLkm_qq() { return lkm_qq; } public void setLkm_qq(String lkm_qq) { this.lkm_qq = lkm_qq; } public String getLkm_mobile() { return lkm_mobile; } public void setLkm_mobile(String lkm_mobile) { this.lkm_mobile = lkm_mobile; } public String getLkm_memo() { return lkm_memo; } public void setLkm_memo(String lkm_memo) { this.lkm_memo = lkm_memo; } public String getLkm_position() { return lkm_position; } public void setLkm_position(String lkm_position) { this.lkm_position = lkm_position; } }View Code
Customer.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="domain" > <class name="Customer" table="cst_customer" > <id name="cust_id" > <generator class="native"></generator> </id> <property name="cust_name" column="cust_name" ></property> <property name="cust_source" column="cust_source" ></property> <property name="cust_industry" column="cust_industry" ></property> <property name="cust_level" column="cust_level" ></property> <property name="cust_linkman" column="cust_linkman" ></property> <property name="cust_phone" column="cust_phone" ></property> <property name="cust_mobile" column="cust_mobile" ></property> <!-- 集合,一對多關係,在配置文件中配置 --> <!-- name屬性:集合屬性名 column屬性: 外鍵列名 class屬性: 與我關聯的對象完整類名 --> <set name="linkMens"> <key column="lkm_cust_id" ></key> <one-to-many class="LinkMan" /> </set> </class> </hibernate-mapping>View Code
LinkMan.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="domain" > <class name="LinkMan" table="cst_linkman" > <id name="lkm_id" > <generator class="native"></generator> </id> <property name="lkm_gender" ></property> <property name="lkm_name" ></property> <property name="lkm_phone" ></property> <property name="lkm_email" ></property> <property name="lkm_qq" ></property> <property name="lkm_mobile" ></property> <property name="lkm_memo" ></property> <property name="lkm_position" ></property> <!-- 多對一 --> <!-- name屬性:引用屬性名 column屬性: 外鍵列名 class屬性: 與我關聯的對象完整類名 --> <many-to-one name="customer" column="lkm_cust_id" class="Customer" ></many-to-one> </class> </hibernate-mapping>View Code
核心配置文件:
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 資料庫驅動 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 資料庫url --> <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property> <!-- 資料庫連接用戶名 --> <property name="hibernate.connection.username">root</property> <!-- 資料庫連接密碼 --> <property name="hibernate.connection.password">xuyiqing</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 將hibernate生成的sql語句列印到控制台 --> <property name="hibernate.show_sql">true</property> <!-- 將hibernate生成的sql語句格式化(語法縮進) --> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 引入orm元數據 路徑書寫: 填寫src下的路徑 --> <property name="hibernate.connection.isolation">4</property> <!-- 指定session與當前線程綁定 --> <property name="hibernate.current_session_context_class">thread</property> <mapping resource="domain/Customer.hbm.xml" /> <mapping resource="domain/LinkMan.hbm.xml" /> </session-factory> </hibernate-configuration>View Code
自定義工具類:
package utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static SessionFactory sf; static{ //1 創建,調用空參構造 Configuration conf = new Configuration().configure(); //2 根據配置信息,創建 SessionFactory對象 sf = conf.buildSessionFactory(); } //獲得session => 獲得全新session public static Session openSession(){ //3 獲得session Session session = sf.openSession(); return session; } //獲得session => 獲得與線程綁定的session public static Session getCurrentSession(){ //3 獲得session Session session = sf.getCurrentSession(); return session; } }View Code
測試類:
package oneTomany; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import domain.Customer; import domain.LinkMan; import utils.HibernateUtils; //一對多|多對一關係操作 public class Demo { @Test //保存客戶 以及客戶 下的聯繫人 public void fun1(){ //1 獲得session Session session = HibernateUtils.openSession(); //2 開啟事務 Transaction tx = session.beginTransaction(); //------------------------------------------------- //3操作 Customer c = new Customer(); c.setCust_name("騰訊"); LinkMan lm1 = new LinkMan(); lm1.setLkm_name("馬化騰"); LinkMan lm2 = new LinkMan(); lm2.setLkm_name("張三"); //表達一對多,客戶下有多個聯繫人 c.getLinkMens().add(lm1); c.getLinkMens().add(lm2); //表達多對一,聯繫人屬於哪個客戶 lm1.setCustomer(c); lm2.setCustomer(c); session.save(c); session.save(lm1); session.save(lm2); //------------------------------------------------- //4提交事務 tx.commit(); //5關閉資源 session.close(); } @Test //為客戶增加聯繫人 public void fun2(){ //1 獲得session Session session = HibernateUtils.openSession(); //2 開啟事務 Transaction tx = session.beginTransaction(); //------------------------------------------------- //3操作 //1> 獲得要操作的客戶對象 Customer c = session.get(Customer.class,1l); //2> 創建聯繫人 LinkMan lm1 = new LinkMan(); lm1.setLkm_name("李四"); //3> 將聯繫人添加到客戶,將客戶設置到聯繫人中 c.getLinkMens().add(lm1); lm1.setCustomer(c); //4> 執行保存 session.save(lm1); //------------------------------------------------- //4提交事務 tx.commit(); //5關閉資源 session.close(); } @Test //為客戶刪除聯繫人 public void fun3(){ //1 獲得session Session session = HibernateUtils.openSession(); //2 開啟事務 Transaction tx = session.beginTransaction(); //------------------------------------------------- //3操作 //1> 獲得要操作的客戶對象 Customer c = session.get(Customer.class,1l); //2> 獲得要移除的聯繫人 LinkMan lm = session.get(LinkMan.class, 3l); //3> 將聯繫人從客戶集合中移除 c.getLinkMens().remove(lm); lm.setCustomer(null); //------------------------------------------------- //4提交事務 tx.commit(); //5關閉資源 session.close(); } }
進階操作一:cascade
上邊fun1代碼這一段:
session.save(c);
session.save(lm1);
session.save(lm2);
這裡發現:如果有很多個聯繫人,依次保存聯繫人代碼量大,有沒有一種方法,在保存客戶的同時保存所有的聯繫人呢?
級聯操作: cascade
save-update: 級聯保存更新
delete:級聯刪除
all:save-update+delete
級聯操作: 簡化操作.目的就是為了少些兩行代碼.
配置文件:
<set name="linkMens" cascade="save-update" > <key column="lkm_cust_id" ></key> <one-to-many class="LinkMan" /> </set>
這裡就做到了級聯保存
同樣還有級聯刪除:刪除客戶,那麼所有該客戶所有聯繫人都將被刪除
註意:客戶和聯繫人配置文件都可以設置cascade屬性
進階操作二:
關係維護屬性:inverse
多表操作的時候,會自動維護雙方,影響效率,這裡可以取消其中的維護以提高效率
在這裡,客戶一方可以不維護(即設置該屬性為true)
<set name="linkMens" inverse="true" cascade="delete" > <key column="lkm_cust_id" ></key> <one-to-many class="LinkMan" /> </set>
inverse屬性: 配置關係是否維護.
true: customer不維護關係
false(預設值): customer維護關係
inverse屬性: 性能優化.提高關係維護的性能.
原則: 無論怎麼放棄,總有一方必須要維護關係.
註意:
一對多關係中: 一的一方放棄.也只能一的一方放棄.多的一方不能放棄.