Hibernate多对多中间表有多个字段字段的注解配置方式(二)

情况二:如果中间表不仅仅是做关联用的,它里面包含了其他字段信息,仅仅靠多对多的关系是搞不定的。

解决方案:多对多的关系拆分为两个一对多!这时候三张表的Model都需要写。

我们知道,一对多的关系,一般都是在多的一方做配置。具体代码如下:

学生表

@Entity
@Table(name = "T_STUDENT")
@SequenceGenerator(name = "SEQ_STUDENT", sequenceName = "SEQ_STUDENT")
public class Student2 implements Serializable {
 private static final long serialVersionUID = 2524659555729848644L;
 private Long id;
 private String name;
 private Date birthday;
 private int sex;
 private String address;
 private Set<TeacherStudent> teacherStudentList;
 @Id
 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_STUDENT")
 @Column(name = "ID", nullable = false, precision = 22, scale = 0)
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 @Column(name = "NAME")
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 @Temporal(TemporalType.DATE)
 @Column(name = "BIRTHDAY")
 public Date getBirthday() {
  return birthday;
 }
 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }
 @Column(name = "sex")
 public int getSex() {
  return sex;
 }
 public void setSex(int sex) {
  this.sex = sex;
 }
 @Column(name = "address")
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 @OneToMany(mappedBy="student",cascade=CascadeType.ALL)
 public Set<TeacherStudent> getTeacherStudentList() {
  return teacherStudentList;
 }
 public void setTeacherStudentList(Set<TeacherStudent> teacherStudentList) {
  this.teacherStudentList = teacherStudentList;
 }
}

教师表

@Entity
@Table(name = "T_TEACHER")
@SequenceGenerator(name = "SEQ_TEACHER", sequenceName = "SEQ_TEACHER")
public class Teacher2 implements Serializable {
 private static final long serialVersionUID = 2297316923535111793L;
 private Long id;
 private String name;
 private int sex;
 private Set<TeacherStudent> teacherStudentList;
 @Id
 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_TEACHER")
 @Column(name = "ID", nullable = false, precision = 22, scale = 0)
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 @Column(name = "name")
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 @Column(name = "sex")
 public int getSex() {
  return sex;
 }
 public void setSex(int sex) {
  this.sex = sex;
 }
 @OneToMany(mappedBy = "teacher",cascade=CascadeType.ALL)
 public Set<TeacherStudent> getTeacherStudentList() {
  return teacherStudentList;
 }
 public void setTeacherStudentList(Set<TeacherStudent> teacherStudentList) {
  this.teacherStudentList = teacherStudentList;
 }
}

中间表

@Entity
@Table(name = "T_TEACHERSTUDENT")
@SequenceGenerator(name = "SEQ_TEACHERSTUDENT", sequenceName = "SEQ_TEACHERSTUDENT")
public class TeacherStudent implements Serializable {
 private Long id;
 private Student2 student;
 private Teacher2 teacher;
 private String note1;
 private String note2;
 @Id
 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_TEACHERSTUDENT")
 @Column(name = "ID", nullable = false, precision = 22, scale = 0)
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 @Column(name = "note1")
 public String getNote1() {
  return note1;
 }
 public void setNote1(String note1) {
  this.note1 = note1;
 }
 @Column(name = "note2")
 public String getNote2() {
  return note2;
 }
 public void setNote2(String note2) {
  this.note2 = note2;
 }
 @ManyToOne(cascade=CascadeType.ALL)
 @JoinColumn(name = "student_id", unique = true)
 public Student2 getStudent() {
  return student;
 }
 public void setStudent(Student2 student) {
  this.student = student;
 }
 @ManyToOne
 @JoinColumn(name = "teacher_id", unique = true)
 public Teacher2 getTeacher() {
  return teacher;
 }
 public void setTeacher(Teacher2 teacher) {
  this.teacher = teacher;
 }
}

hibernate.cfg.xml 引入对象

 <mapping class="com.dvn.li.model.Student2"/>
  <mapping class="com.dvn.li.model.Teacher2"/>
  <mapping class="com.dvn.li.model.TeacherStudent"/>

测试:

SessionFactory sessionFactory = null;
  Session session = null;
  try {
   sessionFactory = HibernateUtil.getSessionFactory();
   session = sessionFactory.getCurrentSession();
   session.beginTransaction();
   Student2 s = new Student2();
   s.setName("小猪");
   Teacher2 t = new Teacher2();
   t.setName("小李");
   TeacherStudent ts=new TeacherStudent();
   ts.setStudent(s);
   ts.setTeacher(t);
   ts.setNote1("以呀呀!!!");
   session.save(s);
   session.save(t);
   session.save(ts);
   session.getTransaction().commit();
  } catch (Exception e) {
   if (session != null) {
    session.getTransaction().rollback();
   }
  }

测试通过!

 

如果自己做测试,可以通过SchemaExport导入表结构

SchemaExport export = new SchemaExport(new AnnotationConfiguration()
    .configure());
  export.create(true, true);

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值