Alex Man :
When I tried to execute the below code I am getting StackOverflow exception
@Test
public void testMappings() {
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("select f from Student f");
System.out.println(q.getResultList());
em.close();
}
I have three entities (Student, Course, CourseRegistration) like as shown below
The exception is as given below
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)
Can anyone please tell me where I went wrong
Full Source code is available in https://github.com/alexman31/jpatest/
UPDATE 1
As per the suggestion from @Anthony and @Nonika I have removed all the lombok annotation and generated explicit setters/ getters, toString, equals and hashcode. But still I am getting the same exception.
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 +
", name='" + name + '\'' +
", 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 +
'}';
}
}
Nonika :
I have build your code and find out that this issue is due to lombok generated hashCode()/equeals()
and toString()
methods
at model.Student.hashCode(Student.java:13)
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
...
...
...
at model.Course.hashCode(Course.java:13)
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
I have removed all lombok annotations (added rating field into the db) and the result is:
[Student{id=123, name='Manu', likedCourses=[Course{id=224, name='Course2'}, Course{id=223, name='Course1'}], registrations=[CourseRegistration{id=1, rating=1}, CourseRegistration{id=2, rating=1}]}, Student{id=124, name='Susan', likedCourses=[Course{id=223, name='Course1'}], registrations=[CourseRegistration{id=3, rating=1}]}, Student{id=125, name='Jacob', likedCourses=[Course{id=224, name='Course2'}, Course{id=223, name='Course1'}], registrations=[CourseRegistration{id=4, rating=1}, CourseRegistration{id=5, rating=1}]}, Student{id=126, name='Mathew', likedCourses=[Course{id=224, name='Course2'}, Course{id=223, name='Course1'}, Course{id=226, name='Course4'}], registrations=[CourseRegistration{id=6, rating=1}, CourseRegistration{id=8, rating=1}, CourseRegistration{id=7, rating=1}]}, Student{id=127, name='Sunny', likedCourses=[], registrations=[]}]
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=227792&siteId=1