hibernate MTM 聯合主鍵
阿新 • • 發佈:2018-01-09
hash ret hbm.xml utf oct import override 映射 ping
//適用於表裏沒有其他列,只有主鍵 //Course.java實體類 package com.tao.pojo; import java.util.HashSet; import java.util.Set; public class Course { private int id; private String name; private Set<Student> stu=new HashSet<Student>(); public Course() { super(); } public Course(intid, String name) { super(); this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) {this.name = name; } public Set<Student> getStu() { return stu; } public void setStu(Set<Student> stu) { this.stu = stu; } @Override public String toString() { return "Course [id=" + id + ", name=" + name + "]"; } } //Student.java實體類package com.tao.pojo; import java.util.HashSet; import java.util.Set; public class Student { private int id; private String name; private String gender; private Set<Course> cou=new HashSet<Course>(); public Student() { super(); } public Student(int id, String name, String gender) { super(); this.id = id; this.name = name; this.gender = gender; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Set<Course> getCou() { return cou; } public void setCou(Set<Course> cou) { this.cou = cou; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", gender=" + gender + "]"; } } //配置文件 //course.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> <class name="com.tao.pojo.Course" table="course"> <id name="id"> <!-- 自動增長 --> <generator class="native"></generator> </id> <property name="name"/> <!-- name:實體來中的屬性名 (Clazz的名稱) clazz:屬性類型(clazz的類型是Clazz)--> <!-- column:外鍵列名稱 --> <!--stu是實體類中set集合的名,sc是表名 , cid是Course在sc表中的外鍵, --> <set name="stu" table="sc"> <key column="cid"></key> <!--set集合當中所存放的對象類型 Student類 sid是Student在sc表中的外鍵 --> <many-to-many class="com.tao.pojo.Student" column="sid"></many-to-many> </set> </class> </hibernate-mapping> //student.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> <class name="com.tao.pojo.Student" table="student"> <id name="id"> <!-- 自動增長 --> <generator class="native"></generator> </id> <!--普通列 --> <property name="name"/> <property name="gender"/> <!-- set集合的配置 sid是Student在sc表中的列名--> <set name="cou" table="sc"> <!-- Student 自己的鍵 --> <key column="sid"></key> <!--set的數據庫類型 cid是Course在sc表中的外鍵 --> <!--Course的鍵 --> <many-to-many class="com.tao.pojo.Course" column="cid"></many-to-many> </set> </class> </hibernate-mapping> //映射文件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="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/test0105_MTM</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 自動創建或更新表(沒有就創建,有就更新 )--> <property name="hbm2ddl.auto">update</property> <!--數據庫方言( 如果外鍵沒有生成就把數據庫方言設置一下 ) --> <!-- org.hibernate.dialect.MySQL5InnoDBDialect 是 mysql數據庫方言的類名 在java文件裏巧 MySQL5 選MySQL5InnoDBDialect --> <!-- 如果執行完了沒報錯,外鍵還是沒有,就把表刪了,再執行一次 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!--配置映射文件 --> <mapping resource="com/tao/pojo/course.hbm.xml"/> <mapping resource="com/tao/pojo/student.hbm.xml"/> </session-factory> </hibernate-configuration> //測試類 //TestJUnit.java package com.tao.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.tao.pojo.Course; import com.tao.pojo.Student; public class TestJUnit { SessionFactory factory ; Session session ; @Before public void test001() { Configuration configure = new Configuration().configure(); factory= configure.buildSessionFactory(); session= factory.openSession(); session.beginTransaction(); } @After public void test002() { session.getTransaction().commit(); session.close(); factory.close(); } @Test public void TestStudent() { } } 測試的時候測試類裏不用寫內容,這裏用TestStudent()測試
hibernate MTM 聯合主鍵