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:
- [Solved] swagger Docmentation Access Error: Illegal DefaultValue 1024 for parameter type integer, java.lang.NumberFormatException
- [Solved] Method threw ‘java.lang.StackOverflowError‘ exception. Cannot evaluate
- [Solved] Java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.util.List
- Java.lang.stackoverflowerror error [How to Solve]
- [Solved] Initialization of anonymous inner class member variable causes java.lang.stackoverflowerror
- Lotti triggered Java lang.StackOverflowError [How to Solve]
- [Solved] IDEA startup Error: Java lang.StackOverflowError
- nested exception is java.lang.StackOverflowError [How to Solve]
- [Solved] nacos Startup Error: nested exception is java.lang.RuntimeException: java.lang.RuntimeException: [db-load-error
- [Solved] Error updating database. Cause: java.sql.SQLException: Incorrect integer value: ‘**‘ for column
- JAVA error: java.lang.OutOfMemoryError:GC overhead limit exceeded
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException
- [Solved] java: Internal error in the mapping processor: java.lang.NullPointerException
- [Solved] Java 9 reflection error: java.lang.reflect.InaccessibleObjectException
- Java error: java.lang.NoSuchMethodError
- [Solved] JAVA OpenCV Startup Error: java.lang.UnsatisfiedLinkError
- [Solved] IDEA springboot Startup Error: java.lang.UnsatisfiedLinkError: no tcnative-1 in java.library.path
- [Solved] JAVA fx Error: java.lang.instrument ASSERTION FAILED ***: “!errorOutstanding“ with message transform
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution