欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实体类@Entity中打印对象时,java.lang.*Error

程序员文章站 2022-07-15 14:21:12
...

1.报错如下(后面省略部分报错,都是重复报错):

遇见的报错信息如下(后面省略,都是重复的报错):
```java
java.lang.*Error
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Employee.toString(Employee.java:29)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Department.toString(Department.java:37)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Employee.toString(Employee.java:29)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Department.toString(Department.java:37)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Employee.toString(Employee.java:29)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Department.toString(Department.java:37)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Employee.toString(Employee.java:29)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Department.toString(Department.java:37)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Employee.toString(Employee.java:29)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Department.toString(Department.java:37)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Employee.toString(Employee.java:29)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Department.toString(Department.java:37)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Employee.toString(Employee.java:29)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Department.toString(Department.java:37)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Employee.toString(Employee.java:29)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Department.toString(Department.java:37)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Employee.toString(Employee.java:29)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Department.toString(Department.java:37)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Employee.toString(Employee.java:29)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Department.toString(Department.java:37)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Employee.toString(Employee.java:29)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at cn.itsource.domain.Department.toString(Department.java:37)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
	at cn.itsource.domain.Department_$$_jvstc24_2.toString(Department_$$_jvstc24_2.java)
........



2.问题分析:
从报错*Error很容易看出,是“栈溢出”的问题,但是我本次的具体情况是这样:
2.1 前提条件:有两个@Entity的实体类:Employee 和 Department
( Employee中关联了Department,Department也关联了Employee。即 JPA中 多对一 的关系)
而且两个类都重写了toString方法,Employee 的toString中有Department对应的字段,而Department中又重写了toString
2.2 报错之处:当使用打印语句打印查询到的Employee时,发生栈溢出。
2.3 原因分析:Employee和Department都关联了对方的属性,Employee 重写的toString中有Department对应的字段,Department重写的toString中又有Employee对应的字段。这样打印一方的对象的时候,就会调用一方的toString,由于里面有对方的对象,就会继续去调用对方对象的toString,对方对象的toString里面又有本方对象,又反过来调用本方toString…自己调用自己,没有出口,最终栈溢出。

Employee实体类的配置关系及toString方法:
实体类@Entity中打印对象时,java.lang.*Error
实体类@Entity中打印对象时,java.lang.*Error
Department的关系配置及toString方法:
实体类@Entity中打印对象时,java.lang.*Error实体类@Entity中打印对象时,java.lang.*Error

最终解决方案:
删除掉一方 或者 双方的 toString中关联对方的字段