加载关系时堆栈溢出
stack overflow when loading relationship
我有以下关系:
@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.WhosebugError: 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 制作的东西有关。
编辑:我注意到有时它确实有效(当我删除整个数据库时)
您可以将 exclude
属性添加到 EqualsAndHashCode
注释中。
例如,要从您的组织实体中排除 repos
collection,请使用:
@EqualsAndHashCode(callSuper = false, exclude={"repos"})
在您组织的构造函数之前 class。
此外,如果有人正在阅读此答案并且碰巧使用 Hibernate,我建议您对 collection 使用 Set
(而不是 List
)。除了避免可怕的 MultipleBagFetchException
之外,Set
允许您在实体中拥有多个急切加载的 collection。它还允许您使用 [left] join fetch
加载多个 collection,如果您有时想使用惰性 v.s。预加载。
我有以下关系:
@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.WhosebugError: 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 制作的东西有关。
编辑:我注意到有时它确实有效(当我删除整个数据库时)
您可以将 exclude
属性添加到 EqualsAndHashCode
注释中。
例如,要从您的组织实体中排除 repos
collection,请使用:
@EqualsAndHashCode(callSuper = false, exclude={"repos"})
在您组织的构造函数之前 class。
此外,如果有人正在阅读此答案并且碰巧使用 Hibernate,我建议您对 collection 使用 Set
(而不是 List
)。除了避免可怕的 MultipleBagFetchException
之外,Set
允许您在实体中拥有多个急切加载的 collection。它还允许您使用 [left] join fetch
加载多个 collection,如果您有时想使用惰性 v.s。预加载。