我正在与 Jpa,Hibernate 一起工作。
我的项目中发生了一件非常奇怪的事情。
我有 2 个实体:学生和课程,多对多关系。
很长一段时间一切都很完美,突然间我遇到了这个问题:
当我找到一个特定的学生(em.find)然后使用getter(student.getCourses)返回它的课程列表时,我只收到列表的一部分!如果我使用查询(“select s.courses from Students s where id = ...”)来执行此操作 - 它会给出正确的结果。
我试图解决它很长时间 - 没有。
任何人都知道问题可能是什么?
任何帮助将不胜感激!
这是我的代码:
/**
* Entity 的实体实现类:Course
*
*/
@Entity(name = "Courses")
公共类课程扩展冲压{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String courseName;
@Version
@Column(name = "Version", nullable = false)
private Long version;
@ManyToMany(mappedBy = "courses",fetch = FetchType.LAZY)
private Set<Student> students = new HashSet<Student>();
@ManyToOne
@JoinColumn(name = "LecturerID")
private Lecturer lecturer;
public Course() {
super();
this.courseName = " ";
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public Lecturer getLecturer() {
return lecturer;
}
public void setLecturer(Lecturer lecturer) {
this.lecturer = lecturer;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
/**
* Entity 的实体实现类:Student
*
*/
@Entity
公共类学生扩展人/和人类扩展冲压/{
private static final long serialVersionUID = 1L;
@Enumerated(EnumType.STRING)
@Column(length = 2)
private E_Year studentYear;
private Boolean inCondition;
@ManyToMany(cascade = PERSIST, fetch = FetchType.LAZY)
private Set<Course> courses = new HashSet<Course>();
public Student() {
super();
this.inCondition = true;
}
public E_Year getStudentYear() {
return studentYear;
}
public void setStudentYear(E_Year studentYear) {
this.studentYear = studentYear;
}
public Boolean getInCondition() {
return inCondition;
}
public void setInCondition(Boolean inCondition) {
this.inCondition = inCondition;
}
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
/**
* Entity 的实体实现类:Stamping
*
*/
@MappedSuperclass
公共类冲压实现可序列化{
private static final long serialVersionUID = 1L;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created", nullable = false)
private Date created;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated", nullable = false)
private Date updated;
private String lastUser;
public Date getCreated() {
return created;
}
public Date getUpdated() {
return updated;
}
public String getLastUser() {
return lastUser;
}
@PrePersist
protected void onCreate() {
updated = created = new Date();
lastUser = System.getProperty("user.name");
}
@PreUpdate
protected void onUpdate() {
updated = new Date();
lastUser = System.getProperty("user.name");
}
public Stamping() {
super();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((created == null) ? 0 : created.hashCode());
result = prime * result
+ ((lastUser == null) ? 0 : lastUser.hashCode());
result = prime * result + ((updated == null) ? 0 : updated.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Stamping other = (Stamping) obj;
if (created == null) {
if (other.created != null)
return false;
} else if (!created.equals(other.created))
return false;
if (lastUser == null) {
if (other.lastUser != null)
return false;
} else if (!lastUser.equals(other.lastUser))
return false;
if (updated == null) {
if (other.updated != null)
return false;
} else if (!updated.equals(other.updated))
return false;
return true;
}
}