当我尝试执行以下代码时,我得到了StackOverflow异常
@Test
public void testMappings() {
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("select f from Student f");
System.out.println(q.getResultList());
em.close();
}
我有三个实体(学生,课程,课程注册),如下所示
例外情况如下
java.lang.StackOverflowError
at org.jboss.logging.Log4jLogger.doLog(Log4jLogger.java:40)
at org.jboss.logging.Logger.debug(Logger.java:385)
at org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:109)
at org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:94)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:181)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:160)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.prepareQueryStatement(AbstractLoadPlanBasedLoader.java:257)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:201)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:100)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:92)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1933)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:558)
谁能告诉我我哪里出问题了
完整源代码在https://github.com/alexman31/jpatest/中可用
更新1
按照 @Anthony 和 @Nonika 的建议,我删除了所有的lombok批注,并生成了显式的setter / getter,toString,equals和hashcode。但是我仍然遇到同样的异常。
Student.java
@Entity
@Table(name = "student")
public class Student {
@Id
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@ManyToMany
@JoinTable(name = "course_registration",joinColumns = @JoinColumn(name = "student_id"),inverseJoinColumns = @JoinColumn(name = "course_id"))
private Set<Course> likedCourses;
@OneToMany(mappedBy = "student")
private Set<CourseRegistration> registrations;
// additional properties
public Student() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
if (getId() != null ? !getId().equals(student.getId()) : student.getId() != null) return false;
if (getName() != null ? !getName().equals(student.getName()) : student.getName() != null) return false;
if (getLikedCourses() != null ? !getLikedCourses().equals(student.getLikedCourses()) : student.getLikedCourses() != null)
return false;
return getRegistrations() != null ? getRegistrations().equals(student.getRegistrations()) : student.getRegistrations() == null;
}
@Override
public int hashCode() {
int result = getId() != null ? getId().hashCode() : 0;
result = 31 * result + (getName() != null ? getName().hashCode() : 0);
result = 31 * result + (getLikedCourses() != null ? getLikedCourses().hashCode() : 0);
result = 31 * result + (getRegistrations() != null ? getRegistrations().hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
",name='" + name + '\'' +
",likedCourses=" + likedCourses +
",registrations=" + registrations +
'}';
}
public Long getId() {
return id;
}
public Set<Course> getLikedCourses() {
return likedCourses;
}
public Set<CourseRegistration> getRegistrations() {
return registrations;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setLikedCourses(Set<Course> likedCourses) {
this.likedCourses = likedCourses;
}
public void setRegistrations(Set<CourseRegistration> registrations) {
this.registrations = registrations;
}
}
Course.java
@Entity
@Table(name = "course")
public class Course {
@Id
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@ManyToMany(mappedBy = "likedCourses")
private Set<Student> likes;
@OneToMany(mappedBy = "course")
private Set<CourseRegistration> registrations;
// additional properties
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Course)) return false;
Course course = (Course) o;
if (getId() != null ? !getId().equals(course.getId()) : course.getId() != null) return false;
if (getName() != null ? !getName().equals(course.getName()) : course.getName() != null) return false;
if (getLikes() != null ? !getLikes().equals(course.getLikes()) : course.getLikes() != null) return false;
return getRegistrations() != null ? getRegistrations().equals(course.getRegistrations()) : course.getRegistrations() == null;
}
@Override
public int hashCode() {
int result = getId() != null ? getId().hashCode() : 0;
result = 31 * result + (getName() != null ? getName().hashCode() : 0);
result = 31 * result + (getLikes() != null ? getLikes().hashCode() : 0);
result = 31 * result + (getRegistrations() != null ? getRegistrations().hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Course{" +
"id=" + id +
",likes=" + likes +
",registrations=" + registrations +
'}';
}
public Course() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getLikes() {
return likes;
}
public void setLikes(Set<Student> likes) {
this.likes = likes;
}
public void setRegistrations(Set<CourseRegistration> registrations) {
this.registrations = registrations;
}
public Set<CourseRegistration> getRegistrations() {
return registrations;
}
}
CourseRegistration.java
@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() {
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Course getcourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CourseRegistration)) return false;
CourseRegistration that = (CourseRegistration) o;
if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) return false;
if (getStudent() != null ? !getStudent().equals(that.getStudent()) : that.getStudent() != null) return false;
return getcourse() != null ? getcourse().equals(that.getcourse()) : that.getcourse() == null;
}
@Override
public int hashCode() {
int result = getId() != null ? getId().hashCode() : 0;
result = 31 * result + (getStudent() != null ? getStudent().hashCode() : 0);
result = 31 * result + (getcourse() != null ? getcourse().hashCode() : 0);
return result;
}
@Override
public String toString() {
return "CourseRegistration{" +
"id=" + id +
",student=" + student +
",course=" + course +
'}';
}
}