嵌套异常是java.lang.StackOverflowError [英] Nested exception is java.lang.StackOverflowError

查看:816
本文介绍了嵌套异常是java.lang.StackOverflowError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂课

@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注释上。但是我不知道是什么。

The problem is probably with the Lombok annotations. But I do not know what.

推荐答案

显然,该异常是由 MovieRate.hashcode( ) MovieEntity.hascode() Lombok ,要解决您的问题,您可以在 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 {
    ...
}

为什么? @Data (因为它使用 @ EqualsAndHashCode )生成 hashCode()

Why? @Data (as it use @EqualsAndHashCode) in order to generate hashCode():


默认情况下,它将使用所有非静态,非瞬态字段

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

也使用 MovieEntity.ratings MovieRate.movi​​e ,并且每次调用 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 生成)方法,这两个实体都将尝试打印另一面。要解决此问题,您可以添加 @ToString 以排除相同的字段。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆