问题描述
我有一堂课
@Entity
@Table(name = "movies")
@Data
public class MovieEntity implements Serializable {
...
@OneToMany(mappedBy = "movie", cascade = CascadeType.ALL)
private Set<MovieRate> ratings;
}
可映射列表
@Entity
@Table(name = "movies_ratings")
@Data
public class MovieRate {
...
}
加载 movie.getRatings()
列表将我拒之门外
...
at org.eclipse.persistence.indirection.IndirectSet.buildDelegate(IndirectSet.java:225) ~[org.eclipse.persistence.core-2.7.0.jar:na]
at org.eclipse.persistence.indirection.IndirectSet.getDelegate(IndirectSet.java:436) ~[org.eclipse.persistence.core-2.7.0.jar:na]
at org.eclipse.persistence.indirection.IndirectSet.hashCode(IndirectSet.java:485) ~[org.eclipse.persistence.core-2.7.0.jar:na]
at com.core.jpa.entity.MovieEntity.hashCode(MovieEntity.java:21) ~[classes/:na]
at com.core.jpa.entity.movie.MovieRate.hashCode(MovieRate.java:16) ~[classes/:na]
at java.util.HashMap.hash(HashMap.java:338) ~[na:1.8.0_144]
at java.util.HashMap.put(HashMap.java:611) ~[na:1.8.0_144]
at java.util.HashSet.add(HashSet.java:219) ~[na:1.8.0_144]
at org.eclipse.persistence.queries.ReadAllQuery.registerResultInUnitOfWork(ReadAllQuery.java:968) ~[org.eclipse.persistence.core-2.7.0.jar:na]
...
所有错误
问题可能出在Lombok注释上。但是我不知道是什么。
The problem is probably with the Lombok annotations. But I do not know what.
推荐答案
显然,该异常是由 MovieRate.hashcode( )
和 MovieEntity.hascode()
由,要解决您的问题,您可以在 MovieRate
或<$ c中添加 @EqualsAndHashCode
$ c> MovieEntity 或两者都:
Apparently, the exception is caused by both MovieRate.hashcode()
and MovieEntity.hascode()
which is generated by Lombok, to solve your issue you may add @EqualsAndHashCode
in MovieRate
or in MovieEntity
or both :
@Entity
@Table(name = "movies")
@Data @EqualsAndHashCode(exclude = "ratings")
public class MovieEntity implements Serializable {
// Your code
}
或
@Entity
@Table(name = "movies_ratings")
@Data @EqualsAndHashCode(exclude = "movie")
public class MovieRate {
...
}
为什么? (因为它使用)生成 hashCode()
:
Why? @Data (as it use @EqualsAndHashCode) in order to generate hashCode()
:
也使用 MovieEntity.ratings
和 MovieRate.movie
,并且每次调用 hashCode()一侧的
方法将调用另一侧的 hashCode()
,并且由于它是双向关联,它将无限运行直到 java.lang.StackOverflowError
。
So it will use MovieEntity.ratings
and MovieRate.movie
as well, and each call of hashCode()
method of the one side will call the other side's hashCode()
, and as it's bidirectional association, it will run infinitely till java.lang.StackOverflowError
.
注意: toString()(也由 @Data
生成)方法,这两个实体都将尝试打印另一面。要解决此问题,您可以添加以排除相同的字段。
Note: You will have the same error for toString()
(which also generated by @Data
) method for both entities, as each one will attempt to print the other side. To solve it, you can add @ToString to exclude the same fields.
这篇关于嵌套异常是java.lang.StackOverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!