首页 > 代码库 > hibernate建表多对多建表

hibernate建表多对多建表

 

Student.java

 1 package cn.itcast.hiberate.sh.domain;
 2 
 3 import java.util.Set;
 4 
 5 public class Student {
 6     private Long sid;
 7     private String sname;
 8     private String description;
 9     Set<Course> courses;
10     
11     
12     public Set<Course> getCourses() {
13         return courses;
14     }
15     public void setCourses(Set<Course> courses) {
16         this.courses = courses;
17     }
18     public Long getSid() {
19         return sid;
20     }
21     public void setSid(Long sid) {
22         this.sid = sid;
23     }
24     public Student(String sname, String description) {
25         super();
26         this.sname = sname;
27         this.description = description;
28     }
29     public Student() {
30         // TODO Auto-generated constructor stub
31     }
32     public String getSname() {
33         return sname;
34     }
35     public void setSname(String sname) {
36         this.sname = sname;
37     }
38     public String getDescription() {
39         return description;
40     }
41     public void setDescription(String description) {
42         this.description = description;
43     }
44     
45 }

Student.hbm.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <class name="cn.itcast.hiberate.sh.domain.Student">
 6         <id name="sid" length="5">
 7             <generator class="increment"></generator>
 8         </id>
 9         <property name="sname" length="20"></property>
10         <property name="description" length="100"></property>
11         <!-- 
12             table就是用来描述第三张表
13             key 用来描述外键,多对多的关系看对哪张表建立映射
14             此表是描述Student信息的,所以 key中 column写sid
15             
16             many-to-many 中用于建立关系的所以 在第三张表中关系用cid建立
17          -->
18         <set name="courses" table="student_course">
19             <key>
20                 <column name="sid"></column>
21             </key>
22             <many-to-many class="cn.itcast.hiberate.sh.domain.Course" column="cid">
23             </many-to-many >
24         </set>
25     </class>
26 </hibernate-mapping>

Course.java

 1 package cn.itcast.hiberate.sh.domain;
 2 
 3 import java.io.Serializable;
 4 import java.util.Set;
 5 
 6 public class Course implements Serializable {
 7     private Long cid;
 8     private String cname;
 9     private String description;
10     private Set<Student> students;
11     public Long getCid() {
12         return cid;
13     }
14     public void setCid(Long cid) {
15         this.cid = cid;
16     }
17     public String getCname() {
18         return cname;
19     }
20     public void setCname(String cname) {
21         this.cname = cname;
22     }
23     public String getDescription() {
24         return description;
25     }
26     public void setDescription(String description) {
27         this.description = description;
28     }
29     public Set<Student> getStudents() {
30         return students;
31     }
32     public void setStudents(Set<Student> students) {
33         this.students = students;
34     }
35     
36 }

Course.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <class name="cn.itcast.hiberate.sh.domain.Course">
 6         <id name="cid" length="5" type="java.lang.Long">
 7             <generator class="increment"></generator>
 8         </id>
 9         <property name="cname" length="20" type="java.lang.String"></property>
10         
11         <property name="description" length="100" type="java.lang.String"></property>
12         <!-- 
13             set元素对应类中的set集合
14             通过set元素使classes表与student表建立关联
15                key是通过外键的形式让两张表建立关联,针对的是哪张表看哪个
16                one-to-many是通过类的形式让两个类建立关联
17             
18             cascade 级联
19                save-update
20                    1、当 保存班级的时候,对学生进行怎么样的操作
21                         如果学生对象在数据库中没有对应的值,这个时候会执行save操作
22                         如果学生对象在数据库中有对应的值,这个时候会执行update操作
23                delete
24                all
25             inverse  维护关系
26                true      不维护关系     
27                false     维护关系
28                default   false
29          -->
30         <set name="students" table="student_course">
31             <!-- 
32                 key是用来描述外键 哪张表就哪個外鍵
33                 <many-to-many> 中的cid 描述类与类之间的关系
34              -->
35             <key>
36                 <column name="cid"></column>
37             </key>
38             <many-to-many class="cn.itcast.hiberate.sh.domain.Student" column="sid">
39             </many-to-many>
40         </set>
41     </class>
42 </hibernate-mapping>

hibernate.cfg.xml

 1 <?xml version=‘1.0‘ encoding=‘utf-8‘?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <!-- 
 7         一个session-factory只能连接一个数据库
 8     -->
 9 <session-factory>
10     <!-- 
11         数据库的用户名
12     -->
13     <property name="connection.username">root</property>
14     <!-- 
15         密码
16     -->
17     <property name="connection.password">friends</property>
18     <!-- 
19         url
20     -->
21     <property name="connection.url">
22         jdbc:mysql://localhost:3306/itcast_sh_hibernate_manyTomany
23     </property>
24     <!-- 
25         作用:根据持久化类和映射文件生成表
26         validate
27         create-drop
28         create
29         update
30     -->
31     <property name="hbm2ddl.auto">update</property>
32     <!-- 
33         显示hibernate内部生成的sql语句
34     -->
35     
36     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
37     <property name="show_sql">true</property>
38     <mapping resource="cn/itcast/hiberate/sh/domain/Course.hbm.xml" />
39     <mapping resource="cn/itcast/hiberate/sh/domain/Student.hbm.xml" />
40 
41 </session-factory>
42 </hibernate-configuration>

test.java

 1 package cn.itcast.hiberate.sh.test;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.Transaction;
 6 import org.hibernate.cfg.Configuration;
 7 import org.junit.Test;
 8 
 9 import cn.itcast.hibernate.sh.utils.HiberanteUtils;
10 
11 public class CreateTable extends HiberanteUtils{
12 
13     @Test
14     public void createTable()
15     {
16         Configuration config=new Configuration();
17         config.configure();
18 //        SessionFactory sessionFactory=config.buildSessionFactory();
19 //        Session session=sessionFactory.openSession();
20 //        Transaction transaction=session.beginTransaction();
21 //        
22 //        
23 //        transaction.commit();
24 //        session.close();
25     }
26 }