[英]ManyToOne relationship does not populate new entity
My goal was to create a new entity to handle the problem with @ManyToMany relationship.我的目标是创建一个新实体来处理@ManyToMany 关系的问题。 I'm wondering to audit new entity but hibernate does not populate the CourseRegistration-new entity with data.
我想审核新实体,但 hibernate 不会用数据填充 CourseRegistration-new 实体。
@Entity
@Table(name = "course_registration")
public class CourseRegistration {
@Id
@Column(name = "id")
private Long id;
@ManyToOne
@JoinColumn(name = "student_id")
private Student student;
@ManyToOne
@JoinColumn(name = "course_id")
private Course course;
public CourseRegistration() {}
}
@Entity
@Table(name = "student")
public class Student {
@Id
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "student")
private List<CourseRegistration> registrations = new ArrayList<>();
public Student() {}
}
@Entity
@Table(name = "course")
public class Course {
@Id
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "course")
private List<CourseRegistration> registrations = new ArrayList<>();
public Course() {}
}
You have to mention fetch type in your mappings and also add getter and setter if not added to your entity like below.您必须在映射中提及获取类型,如果未添加到您的实体中,还需要添加 getter 和 setter,如下所示。
@Entity
@Table(name = "course_registration")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CourseRegistration {
@Id
@Column(name = "id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnore
@JoinColumn(name = "student_id")
private Student student;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnore
@JoinColumn(name = "course_id")
private Course course;
}
@Entity
@Table(name = "student")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
@Id
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "student")
private List<CourseRegistration> registrations = new ArrayList<>();
}
@Entity
@Table(name = "course")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Course {
@Id
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "course")
private List<CourseRegistration> registrations = new ArrayList<>();
}
add below dependency to enable getter and setter with lambok.添加以下依赖项以使用lambok启用getter和setter。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
My bet is that you are missing the cascade option on your @OneToMany
annotation.我敢打赌,您在
@OneToMany
注释上缺少级联选项。 Try the following:请尝试以下操作:
@Entity
@Table(name = "course")
public class Course {
@Id
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "course", cascade = CascadeType.ALL)
private List<CourseRegistration> registrations = new ArrayList<>();
public Course() {}
}
ALL
means that the following operation are propagaed to the associated entity (in this case CourseRegistration
): PERSIST, MERGE, REMOVE, REFRESH and DETACH. ALL
表示将以下操作传播到关联实体(在本例中为CourseRegistration
):PERSIST、MERGE、REMOVE、REFRESH 和 DETACH。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.