標籤:hibernate5 單向關聯 多對多
1.建立項目,項目名稱hibernatedemo14,目錄結構
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M00/8F/5F/wKioL1jcePLTLMmIAAAn_05n5BU281.png-wh_500x0-wm_3-wmp_4-s_2355868735.png" title="QQ20170330111719.png" alt="wKioL1jcePLTLMmIAAAn_05n5BU281.png-wh_50" />
2.在項目中建立lib目錄存放jar檔案,目錄結構
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M01/8F/5F/wKioL1jceTbhPW1VAACKEUKanIM999.png-wh_500x0-wm_3-wmp_4-s_337876409.png" title="QQ20170330111828.png" alt="wKioL1jceTbhPW1VAACKEUKanIM999.png-wh_50" />
3.在src目錄中建立實體類Course,包名(com.mycompany.demo.bean),
650) this.width=650;" src="https://s3.51cto.com/wyfs02/M00/8F/61/wKiom1jceaHDBONEAABMCeh5eZQ532.png-wh_500x0-wm_3-wmp_4-s_3864696538.png" title="QQ20170330111948.png" alt="wKiom1jceaHDBONEAABMCeh5eZQ532.png-wh_50" />
4.實體類Course的內容如下
package com.mycompany.demo.bean;public class Course {private int cid;private String name;public Course() {super();}public Course(String name) {super();this.name = name;}public int getCid() {return cid;}public void setCid(int cid) {this.cid = cid;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
5.在src目錄中建立實體類Course的對應檔Course.hbm.xml,包名(com.mycompany.demo.bean),
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M02/8F/61/wKiom1jcegySYB7NAAA-s0qa5iw548.png-wh_500x0-wm_3-wmp_4-s_1567475686.png" title="QQ20170330112200.png" alt="wKiom1jcegySYB7NAAA-s0qa5iw548.png-wh_50" />
6.對應檔Course.hbm.xml的內容如下
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!--package:指定<class/>所在的包 --><hibernate-mapping package="com.mycompany.demo.bean"><!--name:類名table:表名 catalog:資料庫名稱,預設為hibernate.cfg.xml中配置的資料庫名稱 --> <class name="Course" table="course"> <meta attribute="class-description"> This class contains the course detail. </meta> <!-- name:屬性名稱 colum:列名 --> <id name="cid" type="int" column="cid"> <!-- increment:hibernate維護主索引值 identity:資料庫自增長 sequence:序列 native:根據不同的資料庫選擇建置原則 uuid:通過UUID演算法產生,實際使用較多 assigned:手工設定 --> <generator class="native"/> </id> <!-- length:位元組長度 type:欄位類型,支援java和hibernate類型 not-null:非空約束 unique:唯一性限制式 --> <property name="name" column="name" /> </class></hibernate-mapping>
7.在src目錄中建立實體類Student,包名(com.mycompany.demo.bean),
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/8F/5F/wKioL1jcel7wZFp7AAA_GxHvT_k472.png-wh_500x0-wm_3-wmp_4-s_122025633.png" title="QQ20170330112323.png" alt="wKioL1jcel7wZFp7AAA_GxHvT_k472.png-wh_50" />
8.實體類Student的內容如下
package com.mycompany.demo.bean;import java.util.Set;public class Student {private int sid;private String name;private Set<Course> courses;public Student() {super();}public Student(String name) {super();this.name = name;}public int getSid() {return sid;}public void setSid(int sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Course> getCourses() {return courses;}public void setCourses(Set<Course> courses) {this.courses = courses;}}
9.在src目錄中建立實體類Student的對應檔Student.hbm.xml,包名(com.mycompany.demo.bean),
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/8F/61/wKiom1jcevWz61LAAAA8JK8NOlU218.png-wh_500x0-wm_3-wmp_4-s_1747660130.png" title="QQ20170330112553.png" alt="wKiom1jcevWz61LAAAA8JK8NOlU218.png-wh_50" />
10.對應檔Student.hbm.xml的內容如下
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!--package:指定<class/>所在的包 --><hibernate-mapping package="com.mycompany.demo.bean"><!--name:類名table:表名 catalog:資料庫名稱,預設為hibernate.cfg.xml中配置的資料庫名稱 --> <class name="Student" table="student"> <meta attribute="class-description"> This class contains the student detail. </meta> <!-- name:屬性名稱 colum:列名 --> <id name="sid" type="int" column="sid"> <!-- increment:hibernate維護主索引值 identity:資料庫自增長 sequence:序列 native:根據不同的資料庫選擇建置原則 uuid:通過UUID演算法產生,實際使用較多 assigned:手工設定 --> <generator class="native"/> </id> <!-- length:位元組長度 type:欄位類型,支援java和hibernate類型 not-null:非空約束 unique:唯一性限制式 --> <property name="name" column="name" type="string" length="50" not-null="true" unique="false"/> <set name="courses" table="student_course" cascade="save-update"> <key column="sid"></key> <many-to-many class="Course" column="cid"></many-to-many> </set> </class></hibernate-mapping>
11.在src目錄中建立工具類 HbnUtil,包名(com.mycompany.demo.util),
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M01/8F/61/wKiom1jcezaTqK4gAAAxvi5UdSo944.png-wh_500x0-wm_3-wmp_4-s_2277689490.png" title="QQ20170330112659.png" alt="wKiom1jcezaTqK4gAAAxvi5UdSo944.png-wh_50" />
12.工具類 HbnUtil的內容如下
package com.mycompany.demo.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HbnUtil {private static SessionFactory sessionFactory;public static Session getSession(){if(sessionFactory == null || sessionFactory.isClosed()){sessionFactory = new Configuration().configure().buildSessionFactory();}return sessionFactory.getCurrentSession();}}
13.在src目錄中建立Hibernate的設定檔hibernate.cfg.xml,
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M02/8F/61/wKiom1jcfD2ip5CtAAAsJayjMJ0011.png-wh_500x0-wm_3-wmp_4-s_3399119197.png" title="QQ20170330113123.png" alt="wKiom1jcfD2ip5CtAAAsJayjMJ0011.png-wh_50" />
14.Hibernate的設定檔hibernate.cfg.xml的內容如下
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- 方言,可以從Hibernate核心jar(hibernate-core-x.x.x.Finall.jar) 檔案中的or.hibernate.dialect包中找到相應的類,類的全名就是 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 驅動 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 資料庫連接地址 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <!-- 使用者名稱 --> <property name="hibernate.connection.username"> root </property> <!-- 密碼 --> <property name="hibernate.connection.password"></property> <!-- create:每次都新建立,如果存在就刪除 create-drop:建立新表,sessionFactory關閉,表會刪除 update :表欄位增加,會同步,欄位減少不同步,資料改變會同步修改 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 輸出sql --> <property name="hibernate.show_sql">true</property> <!-- 格式化sql --> <property name="hibernate.format_sql">true</property> <!-- 事務環境一個線程對一個事務 thread:本地事務環境 jta:分散式交易環境 SpringSessionContext:用於ssh整合 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 使用c3p0資料來源 --> <property name="hibernate.connection.provider_class"> org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> <!-- List of XML mapping files --> <mapping resource="com/mycompany/demo/bean/Course.hbm.xml"/> <mapping resource="com/mycompany/demo/bean/Student.hbm.xml"/></session-factory></hibernate-configuration>
15.在項目中建立test目錄存放測試檔案,檔案名稱TestApp,包名(com.mycompany.demo.bean),目錄結構
650) this.width=650;" src="https://s4.51cto.com/wyfs02/M02/8F/61/wKiom1jcfIWDI2RwAAAod-qP2Ac971.png-wh_500x0-wm_3-wmp_4-s_1613483278.png" title="QQ20170330113232.png" alt="wKiom1jcfIWDI2RwAAAod-qP2Ac971.png-wh_50" />
16.TestApp測試類別的內容如下
package com.mycompany.demo.bean;import java.util.HashSet;import java.util.Set;import org.hibernate.Session;import org.junit.Before;import org.junit.Test;import com.mycompany.demo.util.HbnUtil;public class TestApp {private Session session;@Beforepublic void init(){session = HbnUtil.getSession();}/* * 單向關聯-多對多(n:n) */@Testpublic void testManyToMany(){try {session.beginTransaction();Course course1 = new Course("git");Course course2 = new Course("gradle");Set<Course> courses = new HashSet<Course>();courses.add(course1);courses.add(course2);Student student = new Student("xiaoming");student.setCourses(courses);session.save(student);session.getTransaction().commit();} catch (Exception e) {session.getTransaction().rollback();e.printStackTrace();}}}
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/8F/61/wKiom1jcfMDwwT4zAABwCKMO5XE777.png-wh_500x0-wm_3-wmp_4-s_377025596.png" title="QQ20170324102905.png" alt="wKiom1jcfMDwwT4zAABwCKMO5XE777.png-wh_50" />
本文出自 “素顏” 部落格,謝絕轉載!
Hibernate5-單向關聯-多對多(n:n)