Hibernate 休眠多对多仅保存新对象和表之间
我有以下表格: 学生 学生课程 课程 现在,当我创建一个学生时,我只想创建一个用户并向学生课程添加一行。Hibernate 休眠多对多仅保存新对象和表之间,hibernate,Hibernate,我有以下表格: 学生 学生课程 课程 现在,当我创建一个学生时,我只想创建一个用户并向学生课程添加一行。 我遇到的“问题”是,当我将Student中的课程集设置为cascade=“save update”时,也会对课程调用更新。我想知道是否有办法阻止这种行为。如果你真的想要这种行为,你应该把你的@manytomy关系分成@OneToMany manytomone关系 obs:不要忘记机具设定器 public class Student { private Integer id;
我遇到的“问题”是,当我将Student中的课程集设置为cascade=“save update”时,也会对课程调用更新。我想知道是否有办法阻止这种行为。如果你真的想要这种行为,你应该把你的@manytomy关系分成@OneToMany manytomone关系 obs:不要忘记机具设定器
public class Student {
private Integer id;
public Set<StudentCourse> studentCourseSet = new HashSet<StudentCourse>();
@Id
@GeneratedValue
public Integer getId() {
return this.id;
}
/**
* Because you are using a Set collection
* You must provide equals and hashcode implementation in StudentCourse class
*/
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="STUDENT_ID", insertable=false, updatable=false)
public Set<StudentCourse> getStudentCourseSet() {
return this.studentCourseSet;
}
/**
* add convenience method
*/
public void addCourse(Course course) {
getStudentCourseSet().add(new StudentCourseId(getId(), course.getId()));
}
/**
* Feel free to implement your StudentCourse class outside Student one
*/
@Entity
@Table(name="<TABLE_NAME_GOES_HERE>")
public static class StudentCourse {
private StudentCourseId studentCourseId;
public Student student;
public Course course;
/**
* required no-arg constructor
*/
public StudentCourse() {}
public StudentCourse(StudentCourseId studentCourseId) {
this.studentCourseId = studentCourseId;
}
@EmbeddedId
public StudentCourseId getStudentCourseId() {
return this.studentCourseId;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="STUDENT_ID", insertable=false, updatable=false)
public Student getStudent() {
return this.student;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="COURSE_ID", insertable=false, updatable=false)
public Course getCourse() {
return this.course;
}
@Embeddable
public static class StudentCourseId implements Serializable {
private Integer studentId;
private Integer courseId;
/**
* required no-arg constructor
*/
public StudentCourseId() {}
public StudentCourseId(Integer studentId, Integer courseId) {
this.studentId = studentId;
this.courseId = courseId;
}
@Column(name="STUDENT_ID", nullable=false)
public Integer getStudentId() {
return this.studentId;
}
@Column(name="COURSE_ID", nullable=false)
public Integer getCourseId() {
return this.courseId;
}
// required equals and hashcode
public boolean equals(Object o) {
if(o == null)
return false;
if(!(o instanfeof StudentCourseId))
return false;
StudentCourseId other = (StudentCourseId) o;
if(!(getStudentId().equals(o.getStudentId())))
return false;
if(!(getCourseId().equals(o.getCourseId())))
return false;
return false;
}
public int hashcode() {
// hashcode impl goes here
}
}
}
}
公共班级学生{
私有整数id;
public Set studentCourseSet=new HashSet();
@身份证
@生成值
公共整数getId(){
返回此.id;
}
/**
*因为您使用的是集合
*必须在StudentCourse类中提供equals和hashcode实现
*/
@OneToMany(级联=级联类型.ALL)
@JoinColumn(name=“STUDENT\u ID”,insertable=false,updateable=false)
公共设置getStudentCourseSet(){
返回此.studentCourseSet;
}
/**
*添加方便方法
*/
公共课程(课程){
getStudentCourseSet().add(新的StudentCourseId(getId(),course.getId());
}
/**
*请在学生一号之外自由实施您的学生课程
*/
@实体
@表(名称=”)
公共静态课堂学生课程{
私人学生课程ID学生课程ID;
公立学生;
公共课程;
/**
*不需要参数构造函数
*/
公立学生课程(){}
公共学生课程(StudentCourseId StudentCourseId){
this.studentCourseId=studentCourseId;
}
@嵌入ID
public StudentCourseId getStudentCourseId(){
返回此.studentCourseId;
}
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“STUDENT\u ID”,insertable=false,updateable=false)
公立学生getStudent(){
把这个还给我的学生;
}
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“COURSE\u ID”,insertable=false,updateable=false)
公共课程getCourse(){
返回本课程;
}
@可嵌入
公共静态类StudentCourseId实现可序列化{
私人整数学生ID;
私有整数;
/**
*不需要参数构造函数
*/
公立学生课程ID(){}
public StudentCourseId(整数studentId,整数courseId){
this.studentId=studentId;
this.courseId=courseId;
}
@列(name=“STUDENT\u ID”,null=false)
公共整数getStudentId(){
返回此.studentId;
}
@列(name=“COURSE\u ID”,null=false)
公共整数getCourseId(){
返回这个.courseId;
}
//必需的equals和hashcode
公共布尔等于(对象o){
如果(o==null)
返回false;
如果(!(o Instanceof StudentCourseId))
返回false;
StudentCourseId其他=(StudentCourseId)o;
如果(!(getStudentId().equals(o.getStudentId()))
返回false;
如果(!(getCourseId().equals(o.getCourseId()))
返回false;
返回false;
}
公共int hashcode(){
//hashcode impl在这里
}
}
}
}
如果你真的想要这种行为,你应该将你的@manytomy关系拆分为@OneToMany manytomone关系
obs:不要忘记机具设定器
public class Student {
private Integer id;
public Set<StudentCourse> studentCourseSet = new HashSet<StudentCourse>();
@Id
@GeneratedValue
public Integer getId() {
return this.id;
}
/**
* Because you are using a Set collection
* You must provide equals and hashcode implementation in StudentCourse class
*/
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="STUDENT_ID", insertable=false, updatable=false)
public Set<StudentCourse> getStudentCourseSet() {
return this.studentCourseSet;
}
/**
* add convenience method
*/
public void addCourse(Course course) {
getStudentCourseSet().add(new StudentCourseId(getId(), course.getId()));
}
/**
* Feel free to implement your StudentCourse class outside Student one
*/
@Entity
@Table(name="<TABLE_NAME_GOES_HERE>")
public static class StudentCourse {
private StudentCourseId studentCourseId;
public Student student;
public Course course;
/**
* required no-arg constructor
*/
public StudentCourse() {}
public StudentCourse(StudentCourseId studentCourseId) {
this.studentCourseId = studentCourseId;
}
@EmbeddedId
public StudentCourseId getStudentCourseId() {
return this.studentCourseId;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="STUDENT_ID", insertable=false, updatable=false)
public Student getStudent() {
return this.student;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="COURSE_ID", insertable=false, updatable=false)
public Course getCourse() {
return this.course;
}
@Embeddable
public static class StudentCourseId implements Serializable {
private Integer studentId;
private Integer courseId;
/**
* required no-arg constructor
*/
public StudentCourseId() {}
public StudentCourseId(Integer studentId, Integer courseId) {
this.studentId = studentId;
this.courseId = courseId;
}
@Column(name="STUDENT_ID", nullable=false)
public Integer getStudentId() {
return this.studentId;
}
@Column(name="COURSE_ID", nullable=false)
public Integer getCourseId() {
return this.courseId;
}
// required equals and hashcode
public boolean equals(Object o) {
if(o == null)
return false;
if(!(o instanfeof StudentCourseId))
return false;
StudentCourseId other = (StudentCourseId) o;
if(!(getStudentId().equals(o.getStudentId())))
return false;
if(!(getCourseId().equals(o.getCourseId())))
return false;
return false;
}
public int hashcode() {
// hashcode impl goes here
}
}
}
}
公共班级学生{
私有整数id;
public Set studentCourseSet=new HashSet();
@身份证
@生成值
公共整数getId(){
返回此.id;
}
/**
*因为您使用的是集合
*必须在StudentCourse类中提供equals和hashcode实现
*/
@OneToMany(级联=级联类型.ALL)
@JoinColumn(name=“STUDENT\u ID”,insertable=false,updateable=false)
公共设置getStudentCourseSet(){
返回此.studentCourseSet;
}
/**
*添加方便方法
*/
公共课程(课程){
getStudentCourseSet().add(新的StudentCourseId(getId(),course.getId());
}
/**
*请在学生一号之外自由实施您的学生课程
*/
@实体
@表(名称=”)
公共静态课堂学生课程{
私人学生课程ID学生课程ID;
公立学生;
公共课程;
/**
*不需要参数构造函数
*/
公立学生课程(){}
公共学生课程(StudentCourseId StudentCourseId){
this.studentCourseId=studentCourseId;
}
@嵌入ID
public StudentCourseId getStudentCourseId(){
返回此.studentCourseId;
}
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“STUDENT\u ID”,insertable=false,updateable=false)
公立学生getStudent(){
把这个还给我的学生;
}
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“COURSE\u ID”,insertable=false,updateable=false)
公共课程getCourse(){
返回本课程;
}
@可嵌入
公共静态类StudentCourseId实现可序列化{
私人整数学生ID;
私有整数;
/**
*不需要参数构造函数
*/
公立学生课程ID(){}
平民的