Hibernate注解开发详解

 

 

*****************关于注解的简单介绍

详细介绍请点击这里注解详细教程

package com.tomowork.pojo;

import org.hibernate.annotations.GenericGenerator;

import javax.annotation.Generated;
import javax.persistence.*;
import java.io.Serializable;

@Entity() //表明这个类是一个实体类,在数据库中存在对应的字段
@Table(name="emp",schema = "test") //在数据库中对应的表
public class Emp implements Serializable {
    private Integer empno ;
    private String ename ;
    @Id
    /**
     * 可选的主键的生成策略
     * 可选的值:
     TABLE,
     SEQUENCE, //主要用于Oracle数据库
     IDENTITY,
     AUTO; //根据数据库定义主键生成策略,myql底层是自动增长主列
     @GeneratedValue(strategy = GenerationType.AUTO)
     */
    /**
     * 如果主键是指派的,就不能用jpa注解
     */
    @GeneratedValue(generator = "deptno") //定义一个主键生成策略
    @GenericGenerator(name = "deptno",strategy = "assigned")
    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }
    @Column(name = "ename")
    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }
}

****************注解开发单向一对一

  编写数据库脚本:

    假如一个学生只有一部手机,一个手机只属于一个学生;

CREATE TABLE `phone` (
  `tel` varchar(255) NOT NULL DEFAULT '' COMMENT '手机号码',
  `pname` varchar(255) NOT NULL COMMENT '手机名字',
  `price` double NOT NULL COMMENT '手机价格',
  `sid` int(11) NOT NULL COMMENT '手机的主人',
  PRIMARY KEY (`tel`),
  KEY `fk_sid` (`sid`),
  CONSTRAINT `fk_sid` FOREIGN KEY (`sid`) REFERENCES `students` (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `students` ( `sid`
int(11) NOT NULL DEFAULT '0', `sname` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`sid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  编写实体类:

    Students.java

package com.itcloud.pojo;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Entity //表明这是一个数据库实体
@Table(name="Students")
public class Students {
    //统一代码规范将注解设置在get方法上面而不是设置在属性上面
    private Integer sid ;
    private String sname ;
    private Integer age ;
    //无参构造
    public Students(){}
    //全参构造
    public Students(Integer sid, String sname, Integer age) {
        this.sid = sid;
        this.sname = sname;
        this.age = age;
    }

    @Id //表示这是一个主键列
    //定义主键的生成策略,assigned表示是自己指派的
    @GeneratedValue(generator = "sid")
    @GenericGenerator(name="sid",strategy = "assigned")
    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    /**
     *Basic,定义属性的存取获取策略
     *Basic(FetchType.EAGER):默认,表示即时存取
     *Basic(FetchType.LAZY):延迟加载
     */
    @Basic //这里设置默认就可以了
    @Column(length =13,name="sname" )
    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }
    @Basic
    @Column(name = "age")
    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

  Phone.java

package com.itcloud.pojo;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Entity
@Table(name="phone",schema = "test")
public class Phone {
    private String tel ;
    private String pname ;
    private Double price ;
    private Students students ;

    public Phone() {
    }

    public Phone(String tel, String pname, Double price, Students students) {
        this.tel = tel;
        this.pname = pname;
        this.price = price;
        this.students = students;
    }

    @Id
    @GeneratedValue(generator = "tel")
    @GenericGenerator(name = "tel",strategy="assigned") //自己指派主键生成策略
    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }
    @Basic
    @Column(name="pname")
    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }
    @Basic
    @Column(name="price")
    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    /**
     *进行单项一对一的关联映射
     */

    @OneToOne(cascade = CascadeType.ALL) //全部级联CRUD操作
    @JoinColumn(name="sid",unique = true) //name指的是主表的外键,unique表示是否是唯一的
    public Students getStudents() {
        return students;
    }

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

  测试用例:

package com.itcloud.test;

import com.itcloud.pojo.Phone;
import com.itcloud.pojo.Students;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Test;

public class TestHibernate {
    public static SessionFactory sessionFactory =null ;
    public static Session session = null ;
    public static Transaction transaction ;
    static {
        Configuration configuration = new Configuration().configure() ;
        sessionFactory = configuration.buildSessionFactory() ;
        session = sessionFactory.openSession() ;
        transaction = session.beginTransaction() ;
    }
    @Test
    public void post(){
        //创建学生实体
        Students stus = new Students(201401,"张三",10) ;
        Phone phone = new Phone("13588578866","iphone",888.36,stus) ;
        session.save(stus) ;
        session.save(phone) ;
    }

    @After
    public void destroy(){
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
}

  测试结果:

Hibernate: 
    insert 
    into
        Students
        (age, sname, sid) 
    values
        (?, ?, ?)

Hibernate: 十二月 02, 2017 12:19:41 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
    insert 
    into

        phoneINFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8]
        (pname, price, sid, tel) 
    values
        (?, ?, ?, ?)

***********注解开发一对一双向

数据库表结构

CREATE TABLE `students` (
`sid` int(11) NOT NULL,
`sname` varchar(15) NOT NULL DEFAULT '',
`age` int(11) NOT NULL DEFAULT '0',
`tel` varchar(18) NOT NULL DEFAULT '' COMMENT '一个学生有一部手机',
PRIMARY KEY (`sid`),
KEY `fk_tel` (`tel`),
CONSTRAINT `fk_tel` FOREIGN KEY (`tel`) REFERENCES `phone` (`tel`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `phone` (
`tel` varchar(18) NOT NULL,
`pname` varchar(20) NOT NULL DEFAULT '' COMMENT '手机名字',
`price` double NOT NULL DEFAULT '0' COMMENT '手机价格',
PRIMARY KEY (`tel`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    假如一个学生拥有一个手机,一个手机属于一个学生,学生为主控方

    实体类的编写

      Students

package com.itcloud.pojo;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;

@Entity //表明这是一个数据库实体
@Table(name="students")
public class Students implements Serializable {
    //统一代码规范将注解设置在get方法上面而不是设置在属性上面
    private Integer sid ;
    private String sname ;
    private Integer age ;
    private Phone phone  ;//一个学生拥有一个手机
    //无参构造
    public Students(){}
    //全参构造
    public Students(Integer sid, String sname, Integer age) {
        this.sid = sid;
        this.sname = sname;
        this.age = age;
    }

    @Id //表示这是一个主键列
    //定义主键的生成策略,assigned表示是自己指派的
    @GeneratedValue(generator = "sid")
    @GenericGenerator(name="sid",strategy = "assigned")
    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    /**
     *Basic,定义属性的存取获取策略
     *Basic(FetchType.EAGER):默认,表示即时存取
     *Basic(FetchType.LAZY):延迟加载
     */
    @Basic //这里设置默认就可以了
    @Column(name="sname" )
    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }
    @Basic
    @Column(name = "age")
    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }


   @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
   @JoinColumn(name="tel",unique = true)  //name指的是主表的外键,数据库中的外键,unique表示是否是唯一的
    public Phone getPhone() {
        return phone;
    }
    public void setPhone(Phone phone) {
        this.phone = phone;
    }
}

    Phones.java

 1 package com.itcloud.pojo;
 2 
 3 import org.hibernate.annotations.GenericGenerator;
 4 
 5 import javax.persistence.*;
 6 import java.io.Serializable;
 7 
 8 @Entity
 9 @Table(name="phone",schema = "test")
10 public class Phone implements Serializable {
11     private String tel ;
12     private String pname ;
13     private Double price ;
14 
15     private Students students ; //一个手机属于一个学生
16 
17     public Phone() {
18     }
19 
20     public Phone(String tel, String pname, Double price) {
21         this.tel = tel;
22         this.pname = pname;
23         this.price = price;
24     }
25 
26     @Id
27     @GeneratedValue(generator = "tel")
28     @GenericGenerator(name = "tel",strategy="assigned") //自己指派主键生成策略
29     public String getTel() {
30         return tel;
31     }
32 
33     public void setTel(String tel) {
34         this.tel = tel;
35     }
36     @Basic
37     @Column(name="pname")
38     public String getPname() {
39         return pname;
40     }
41 
42     public void setPname(String pname) {
43         this.pname = pname;
44     }
45     @Basic
46     @Column(name="price")
47     public Double getPrice() {
48         return price;
49     }
50 
51     public void setPrice(Double price) {
52         this.price = price;
53     }
54 
55     @OneToOne(mappedBy = "phone",fetch = FetchType.LAZY) //主表类中的从表实例
56     public Students getStudents() {
57         return students;
58     }
59 
60     public void setStudents(Students students) {
61         this.students = students;
62     }
63 }

测试用例:

  @Test
    public void post(){
        //创建学生实体
        Phone phone = new Phone("13588578867","iphone",888.36) ;
        Students stus = new Students(201402,"李四",10) ;
        stus.setPhone(phone);
        phone.setStudents(stus);
        session.save(phone) ; //一定要保存手机,再保存学生
        session.save(stus) ;
    }

双向一对一总结:

  首先表设计:主表:students,存在有phone的外键

        从表:没有外键,数据交给students管理;

  实体类的设计:主表:        

          @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
           @JoinColumn(name="tel",unique = true)  //name指的是主表的外键,数据库中的外键,unique表示是否是唯一的

         从表:

    @OneToOne(mappedBy = "phone",fetch = FetchType.LAZY) //phone为主表类中的从表实例即:private Phone phone ;中的phone

 ***********单方多对一关联关系:

  假设一个部门拥有多个雇员,一个雇员属于一个部门,典型的多对一关联

   首先表设计:  

CREATE TABLE `dept` (
  `deptno` int(11) NOT NULL DEFAULT '0',
  `dname` varchar(255) DEFAULT '',
  `loc` varchar(255) DEFAULT '',
  PRIMARY KEY (`deptno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `emp` (
  `empno` int(11) NOT NULL,
  `ename` varchar(15) NOT NULL DEFAULT '' COMMENT '雇员姓名',
  `job` varchar(25) NOT NULL DEFAULT '' COMMENT '雇员职位',
  `deptno` int(11) NOT NULL DEFAULT '-1' COMMENT '所属部门,-1表示没有部门',
  PRIMARY KEY (`empno`),
  KEY `fk_deptno` (`deptno`),
  CONSTRAINT `fk_deptno` FOREIGN KEY (`deptno`) REFERENCES `dept` (`deptno`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

实体类的编写

  Dept.java(多方)

package com.itcloud.pojo;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
/**
 * @author:itcloud
 *
 */
public class Dept {
    private Integer deptno;
    private String dname;
    private String loc;

    public Dept() {
    }

    public Dept(Integer deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    @Id
    @Column(name = "deptno")
    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    @Basic
    @Column(name = "dname")
    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    @Basic
    @Column(name = "loc")
    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {return true;}
        if (o == null || getClass() != o.getClass()){ return false;}

        Dept dept = (Dept) o;

        if (deptno != dept.deptno) {return false;}
        if (dname != null ? !dname.equals(dept.dname) : dept.dname != null) {return false;}
        if (loc != null ? !loc.equals(dept.loc) : dept.loc != null) {return false;}

        return true;
    }

    @Override
    public int hashCode() {
        int result = deptno;
        result = 31 * result + (dname != null ? dname.hashCode() : 0);
        result = 31 * result + (loc != null ? loc.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}';
    }
}

Emp.java(一方)

package com.itcloud.pojo;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Entity
@Table(name = "emp",schema = "test")
public class Emp {

    private Integer empno ;
    private String ename  ;
    private String job ;

    private Dept dept ;

    public Emp() {
    }

    public Emp(Integer empno, String ename, String job, Dept dept) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.dept = dept;
    }

    /**
     * emp是多方,dept是一方
     *
     */
    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER) //抓取策略是积极的
    @JoinColumn(name = "deptno")//deptno是外键
    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    @Id
    @GeneratedValue(generator = "empno")
    @GenericGenerator(name="empno",strategy = "assigned")
    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }
    @Column(name="ename")
    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }
    @Column(name="job")
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }
}

测试用例:

    @Test
    public void post(){
        //多对一测试
        Dept dept = new Dept(11,"运维部门","402");
        Emp emp = new Emp(7371,"clom","linux运维",dept) ;
        session.save(dept) ;
        session.save(emp) ;
    }

测试结果:

Hibernate: 
    insert 
    into
        Dept
        (dname, loc, deptno) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        emp
        (deptno, ename, job, empno) 
    values
        (?, ?, ?, ?)

多对一在多方(即Emp.java)上面添加如下注解

...   
/** * emp是多方,dept是一方 * */ @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER) //抓取策略是积极的 @JoinColumn(name = "deptno")//deptno是外键 public Dept getDept() { return dept; }
...

***********一对多单向外键关联

 一方持有多方的集合 :一个部门存在多个雇员,但是雇员不是重复的,在Dept.java中添加:private  Set<Emp> allEmps = new HashSet<>() ;

数据表:还是dept,emp这两个表

编写实体类

Dept.java

package com.itcloud.pojo;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "dept")
public class Dept {

    private Integer deptno ;
    private String dname ;
    private String loc ;

    private Set<Emp> emps = new HashSet<>() ;

    @Id
    @GeneratedValue(generator = "dno")
    @GenericGenerator(name="dno",strategy = "assigned")
    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumn(name = "deptno",updatable = false)
    public Set<Emp> getEmps() {
        return emps;
    }

    public void setEmps(Set<Emp> emps) {
        this.emps = emps;
    }
}

 

Emp.java

 

package com.itcloud.pojo;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="emp")
public class Emp {

    private Integer empno ;
    private String ename ;
    private String job ;

    public Emp() {
    }

    public Emp(Integer empno, String ename, String job) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
    }

    @Id
    @GeneratedValue(generator = "eno")
    @GenericGenerator(name = "eno",strategy = "assigned")
    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }
}

测试用例:

 @Test
    public void post(){
        Dept dept = new Dept() ;
        dept.setDeptno(11);
        dept.setDname("开发部门");
        dept.setLoc("403");

        Emp emp1 = new Emp(7777,"smith","java开发") ;
        Emp emp2 = new Emp(7778,"jack","后端开发") ;

        dept.getEmps().add(emp1) ;
        dept.getEmps().add(emp2) ;

        session.save(emp1) ;
        session.save(emp2) ;
        session.save(dept) ;


    }

 ***********1-N(N-1)双向外键关联

package com.itcloud.pojo;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
public class Dept {
    private int deptno;
    private String dname;
    private String loc;
    private Set<Emp> emps = new HashSet<>();

    @Id
    @Column(name = "deptno", nullable = false)
    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    @Basic
    @Column(name = "dname", nullable = true, length = 255)
    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    @Basic
    @Column(name = "loc", nullable = true, length = 255)
    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Dept dept = (Dept) o;

        if (deptno != dept.deptno) return false;
        if (dname != null ? !dname.equals(dept.dname) : dept.dname != null) return false;
        if (loc != null ? !loc.equals(dept.loc) : dept.loc != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = deptno;
        result = 31 * result + (dname != null ? dname.hashCode() : 0);
        result = 31 * result + (loc != null ? loc.hashCode() : 0);
        return result;
    }

    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumn(name="deptno")
    public Set<Emp> getEmps() {
        return emps;
    }

    public void setEmps(Set<Emp> emps) {
        this.emps = emps;
    }
}
package com.itcloud.pojo;

import javax.persistence.*;

@Entity
public class Emp {
    private int empno;
    private String ename;
    private String job;
    private Dept dept;

    public Emp(int empno, String ename, String job) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
    }

    @Id
    @Column(name = "empno", nullable = false)
    public int getEmpno() {
        return empno;
    }

    public void setEmpno(int empno) {
        this.empno = empno;
    }

    @Basic
    @Column(name = "ename", nullable = true, length = 255)
    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    @Basic
    @Column(name = "job", nullable = true, length = 255)
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Emp emp = (Emp) o;

        if (empno != emp.empno) return false;
        if (ename != null ? !ename.equals(emp.ename) : emp.ename != null) return false;
        if (job != null ? !job.equals(emp.job) : emp.job != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = empno;
        result = 31 * result + (ename != null ? ename.hashCode() : 0);
        result = 31 * result + (job != null ? job.hashCode() : 0);
        return result;
    }

    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name = "deptno", referencedColumnName = "deptno")
    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }
}

注意點:

  1.无论是在多方还是在一方,都要加上:@JoinColumn(name="deptno")这一语句,如果不加则无法级联,deptno为多方的外键,

  2.不要加mappedBy这一属性,因为加上这一属性只可以从多方进行级联,而一方无法进行级联,就需要设置①,②两个字段

測試:


 @Test
public void post(){
Dept dept = new Dept() ;
dept.setDeptno(11);
dept.setDname("开发部门");
dept.setLoc("313");

Emp emp1 = new Emp(7777,"smith","java开发") ;
Emp emp2 = new Emp(7778,"jack","后端开发") ;

dept.getEmps().add(emp1) ;
dept.getEmps().add(emp2) ;
// emp1.setDept(dept);//②
// emp2.setDept(dept);//①
session.save(emp1) ;
session.save(emp2) ;
session.save(dept) ;

}

結果

Hibernate: 
    insert 
    into
        Emp
        (deptno, ename, job, empno) 
    values
        (?, ?, ?, ?)
Hibernate: 
    insert 
    into
        Emp
        (deptno, ename, job, empno) 
    values
        (?, ?, ?, ?)
Hibernate: 
    insert 
    into
        Dept
        (dname, loc, deptno) 
    values
        (?, ?, ?)
Hibernate: 
    update
        Emp 
    set
        deptno=? 
    where
        empno=?
Hibernate: 
    update
        Emp 
    set
        deptno=? 
    where
        empno=?

************多对多外键关联

表结构:

CREATE TABLE `teachers` (
  `tid` int(11) NOT NULL,
  `tname` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `students` (
  `sid` int(11) NOT NULL,
  `sname` varchar(15) NOT NULL DEFAULT '',
  PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


-- 中间关系维护表
CREATE TABLE `stu_teach` (
  `tid` int(11) NOT NULL,
  `sid` int(11) NOT NULL,
  KEY `FKt56181amkhrdpmd2ici8ngp35` (`sid`),
  KEY `FKe9ifjo0m9y756vlrgmb8oygsa` (`tid`),
  CONSTRAINT `FKe9ifjo0m9y756vlrgmb8oygsa` FOREIGN KEY (`tid`) REFERENCES `teachers` (`tid`),
  CONSTRAINT `FKt56181amkhrdpmd2ici8ngp35` FOREIGN KEY (`sid`) REFERENCES `students` (`sid`),-- 这里面的外键是开发工具加的,不知道为啥mmp
  CONSTRAINT `fk_sid` FOREIGN KEY (`sid`) REFERENCES `students` (`sid`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `fk_tid` FOREIGN KEY (`tid`) REFERENCES `teachers` (`tid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@ManyToMany
    @JoinTable(
            name = "stu_teach",
            joinColumns = {@JoinColumn(name = "tid",updatable = false)},
            inverseJoinColumns = {@JoinColumn(name="sid",updatable = false)}
    )
    public Set<Students> getStus() {
        return stus;
    }

在Teachers.java类中上面的注解即可满足单向多对多开发;

***********双向多对多

Students.java

    private Set<Teachers> teachers = new HashSet<>() ;
    @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinTable(
            name = "stu_teach",
            joinColumns = {@JoinColumn(name = "sid",updatable = false)},//该实体对应表的主键
            inverseJoinColumns = {@JoinColumn(name="tid",updatable = false)}
    )
    public Set<Teachers> getTeachers() {
        return teachers;
    }

Teacher.java

 private Set<Students> stus = new HashSet<>();
    @ManyToMany(cascade = CascadeType.MERGE,fetch = FetchType.LAZY)
    @JoinTable(
            name = "stu_teach",
            joinColumns = {@JoinColumn(name = "tid",updatable = false)},//该实体对应表的主键
            inverseJoinColumns = {@JoinColumn(name="sid",updatable = false)}
    )
    public Set<Students> getStus() {
        return stus;
    }

 

总结:

真的很操蛋啊,比mybatis要难学多了,各种bug,哎

posted @ 2017-12-03 13:43  小洋编程  阅读(3696)  评论(0编辑  收藏  举报