gpt4 book ai didi

java - 嵌套异常是java.lang.StackOverflowError

转载 作者:行者123 更新时间:2023-11-30 02:20:14 25 4
gpt4 key购买 nike

我有课

@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]
...

全错了https://pastebin.com/RgNg84Cb

问题可能出在 Lombok 注释上。但我不知道是什么。

最佳答案

显然,该异常是由 Lombok 生成的 MovieRate.hashcode()MovieEntity.hascode() 引起的。 ,要解决您的问题,您可以在 MovieRateMovieEntity 或两者中添加 @EqualsAndHashCode :

@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 {
...
}
为什么? @Data (因为它使用 @EqualsAndHashCode )为了生成 hashCode():

By default, it'll use all non-static, non-transient fields

因此它也会使用 MovieEntity. ratingsMovieRate.movi​​e ,并且每次调用一侧的 hashCode() 方法会调用对方的hashCode(),由于是双向关联,所以会无限运行,直到java.lang.StackOverflowError

注意:对于两个实体的 toString() (也由 @Data 生成)方法,您将遇到相同的错误,如下所示每个人都会尝试打印另一面。要解决它,可以添加 @ToString排除相同的字段。

关于java - 嵌套异常是java.lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47059019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com