加载关系时堆栈溢出

时间:2016-08-26 08:30:32

标签: jpa eclipselink lombok

我有以下关系:

@Data
@EqualsAndHashCode(callSuper=true)
public class Room extends BasePersistable {

    private static final long serialVersionUID = 1L;

    @Column(nullable=false)
    private String uid;

    @Column(nullable=false)
    private String name;

    @Column(nullable=true)
    private String fullName;

    @Column(nullable=true, length=500)
    private String description;

    @Column(nullable=true)
    private String homepage;

    @Column(nullable=false)
    private String owner;

    @ManyToOne
    @JsonIgnoreProperties({"repos", "members"})
    private Organization organization;

@Data
@EqualsAndHashCode(callSuper=true)
@JsonInclude(content=Include.NON_NULL)
public class Organization extends BasePersistable {
    private static final long serialVersionUID = -7119760968529447945L;

    @Column(nullable=false)
    @NotEmpty
    private String name;

    @Column(nullable=false, unique = true)
    @NotNull
    private Integer uid;

    @Column
    private String avatar;

    @Column
    private String blog;

    @Column
    private String company;

    @Column(nullable=false)
    @NotNull
    private String login;

    @Column(nullable=true)
    private String location;

    @OneToMany(mappedBy="organization")
    @JsonIgnoreProperties({"users", "joined", "organization"})
    private List<Room> repos = new ArrayList<>();

当我从特定用户加载房间时,我收到此错误:

java.lang.StackOverflowError: null
    at com.inkdrop.app.domain.models.Organization.hashCode(Organization.java:29) ~[classes/:na]
    at com.inkdrop.app.domain.models.Room.hashCode(Room.java:34) ~[classes/:na]
    at java.util.AbstractList.hashCode(AbstractList.java:541) ~[na:1.8.0_51]
    at java.util.Vector.hashCode(Vector.java:992) ~[na:1.8.0_51]
    at org.eclipse.persistence.indirection.IndirectList.hashCode(IndirectList.java:504) ~[org.eclipse.persistence.core-2.6.1.jar:na]
    at com.inkdrop.app.domain.models.Organization.hashCode(Organization.java:29) ~[classes/:na]
    at com.inkdrop.app.domain.models.Room.hashCode(Room.java:34) ~[classes/:na]
    at java.util.AbstractList.hashCode(AbstractList.java:541) ~[na:1.8.0_51]
    at java.util.Vector.hashCode(Vector.java:992) ~[na:1.8.0_51]
    at org.eclipse.persistence.indirection.IndirectList.hashCode(IndirectList.java:504) ~[org.eclipse.persistence.core-2.6.1.jar:na]
    at com.inkdrop.app.domain.models.Organization.hashCode(Organization.java:29) ~[classes/:na]
    at com.inkdrop.app.domain.models.Room.hashCode(Room.java:34) ~[classes/:na]
    at java.util.AbstractList.hashCode(AbstractList.java:541) ~[na:1.8.0_51]
    at java.util.Vector.hashCode(Vector.java:992) ~[na:1.8.0_51]
    at org.eclipse.persistence.indirection.IndirectList.hashCode(IndirectList.java:504) ~[org.eclipse.persistence.core-2.6.1.jar:na]
    at com.inkdrop.app.domain.models.Organization.hashCode(Organization.java:29) ~[classes/:na]
    at com.inkdrop.app.domain.models.Room.hashCode(Room.java:34) ~[classes/:na]
....

我不确定问题是否与Eclipselink或Lombok所做的事情有关。

编辑:我注意到有时它确实有效(当我删除整个数据库时)

1 个答案:

答案 0 :(得分:1)

您可以在exclude注释中添加EqualsAndHashCode属性。 例如,要从您的组织实体中排除repos集合,请使用:

@EqualsAndHashCode(callSuper = false, exclude={"repos"})

在Organization类的构造函数之前。

此外,如果有人正在阅读此答案并且碰巧使用Hibernate,我建议您使用Set(而不是List)来收集。除了避免可怕的MultipleBagFetchExceptionSet允许您在实体中拥有多个急切加载的集合。它还允许您使用[left] join fetch加载多个集合,如果您有时想使用懒惰的vs.急切的装载。