[Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)

1. Problem Description:

When using hibernate to query the database, the data has been successfully found from the database, but an error is reported when looping through the output: java.lang.stackoverflowerror

2. Solution process:

2.1 this is my query method:

@Test
public void test01() {
     Session session = SessionFactoryUtil.getSessionFactory().openSession();
     String hql = "from Student";

     Query query = session.createQuery(hql);
     List list = query.list();
     list.forEach(System.out::println);
 }

2.2 this is an error message:

java.lang.StackOverflowError
	at java.lang.Integer.toString(Integer.java:402)
	at java.lang.Integer.toString(Integer.java:935)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at Course.toString(Course.java:16)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at java.util.AbstractCollection.toString(AbstractCollection.java:462)
	at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at Student.toString(Student.java:25)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at java.util.AbstractCollection.toString(AbstractCollection.java:462)
	at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at Course.toString(Course.java:16)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at java.util.AbstractCollection.toString(AbstractCollection.java:462)
	at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at Student.toString(Student.java:25)
	......loop

2.3 check the error message and find that the top exception points to my Lombok annotation: @data

2.4 it is suspected that hibernate does not support Lombok

So I changed all the annotations to setter, getter and toString methods
and then reported the same error. This time, I pointed to the line of outputting courselist in the toString method of my student class

2.5 take a look at my two entity classes:

Student class:

@Data
@NoArgsConstructor
@AllArgsConstructor
@ManagedBean(name = "student")
@Entity(name = "student")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    @ManyToMany
    private List<Course> courseList;

}

Course class:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "course")
@ManagedBean(name = "course")
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;
    private Integer no;

    @ManyToMany(mappedBy = "courseList")
    private List<Student> studentList;
    
}

Note: student and course are many to many, so each class has a collection of each other

2.6 finding problems:

It is not difficult to find that there is a course collection courselist in the student class, and there is also a student collection studentlist in the course class. Therefore, when outputting, it enters a wireless loop process, which is briefly described as follows:

student -> course -> student->course...

3. Solution:

Delete the line that outputs studentlist in the tostring() method in the Course class, and only keep the line that outputs courselist in the student class. Or just keep the output of studentlist in the Course class

Read More: