Hibernate初学者---多对多双向关联数据表的CRUD增删改查

主要掌握JoinTable的格式,以及@cascadeType 的选择。

POJO类一

package com.bak.bum.union;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "200_teacher")
public class Teacher {
    private int id;
    private String name;
    Set students = new HashSet();

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "tsTable", joinColumns = { @JoinColumn(name = "TeacherID") }, inverseJoinColumns = {
            @JoinColumn(name = "StudentsID") })
    public Set getStudents() {
        return students;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setStudents(Set students) {
        this.students = students;
    }

}

POJO类二:

package com.bak.bum.union;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "200_students")
public class Students {

    private int id;
    private String name;
    private Set teacher = new HashSet();

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @ManyToMany(mappedBy="students",cascade=CascadeType.ALL)
    public Set getTeacher() {
        return teacher;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setTeacher(Set teacher) {
        this.teacher = teacher;
    }

}

Junit TEST类

package com.bak.fan.test;

import java.util.EnumSet;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import com.bak.bum.union.Students;
import com.bak.bum.union.Teacher;

public class Test {

    private static SessionFactory sessionFactory = null;

    @BeforeClass
    public static void BeforeClass() {

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
        Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
        SchemaExport schemaExport = new SchemaExport();
        schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);

        Configuration cfg = new Configuration().configure();
        sessionFactory = cfg.buildSessionFactory();

    }

    @AfterClass
    public static void AfterClass() {
        sessionFactory.close();
    }

    @org.junit.Test
    public void test() {

        Session s1= sessionFactory.openSession();
        s1.beginTransaction();
        Teacher t = new Teacher();
        Teacher t1 = new Teacher();
        t.setName("王老师");
        t1.setName("张老师");

        Students st=new Students();
        Students st1=new Students();
        st.setName("小草");
        st1.setName("小花");

        t.getStudents().add(st);
        t.getStudents().add(st1);
        t1.getStudents().add(st);
        s1.save(t);
        s1.save(t1);

        s1.getTransaction().commit();
        s1.close();




    }
}

hibernate.cfg.xml

  



<hibernate-configuration>
    <session-factory>

        <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>

        <property name="connection.url">jdbc:mysql://localhost/hibernateproperty>

        <property name="connection.username">rootproperty>

        <property name="connection.password">shwythnn00property>

        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialectproperty>

        <property name="show_sql">trueproperty>

        <property name="hibernate.format_sql">trueproperty>

        <property name="hibernate.hbm2ddl.auto">createproperty>

        <property name="">property>


        <mapping class="com.bak.bum.union.Teacher" />
        <mapping class="com.bak.bum.union.Students" />
    session-factory>
hibernate-configuration>  

输出结果
Hibernate初学者---多对多双向关联数据表的CRUD增删改查_第1张图片

SHOW DB;
Hibernate初学者---多对多双向关联数据表的CRUD增删改查_第2张图片

JAR
Hibernate初学者---多对多双向关联数据表的CRUD增删改查_第3张图片
Hibernate初学者---多对多双向关联数据表的CRUD增删改查_第4张图片

你可能感兴趣的:(hibernate)