Spring 多对对实体
2020-12-13 06:16
阅读:269
标签:type nts class util 学生 inf 关系 info str
package com.wangshenghua.entity; import java.io.Serializable; 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 = "course") public class Course implements Serializable { private static final long serialVersionUID = 5844590076435775458L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name;
/**
* 一个学生对应多门功课
* 多对多时,使用中间表关联两者关系
*/
@ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "student_has_course", joinColumns = @JoinColumn(name = "course_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "student_id", referencedColumnName = "id")) private Setstudents; // @JoinTable里的 name 中间表 // joinColumns 当前实体ID // inverseJoinColumns对方实体ID public Course() { } public Course(String name) { super(); 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 getStudents() { return students; } public void setStudents(Set students) { this.students = students; } @Override public String toString() { return "Course [id=" + id + ", name=" + name + ", students=" + students + "]"; } }
Spring 多对对实体
标签:type nts class util 学生 inf 关系 info str
原文地址:https://www.cnblogs.com/max-hou/p/11172952.html
评论
亲,登录后才可以留言!