码迷,mamicode.com
首页 > Web开发 > 详细

hibernate MTM 联合主键

时间:2018-01-08 22:28:27      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:不用   factor   更新   resource   blog   dtd   实体类   ati   适用于   

//适用于表里没有其他列,只有主键
//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(int id, 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 联合主键

标签:不用   factor   更新   resource   blog   dtd   实体类   ati   适用于   

原文地址:https://www.cnblogs.com/jili6254/p/8245072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!