英文:
JPA and Lombok causing StackOverflowError in circular dependency
问题
我有两个实体类:
@Data
@Entity(name = "user")
@Table(name = "tbl_user")
@EqualsAndHashCode(callSuper = true, exclude = "products")
public class UserEntity extends BaseEntity {
@Column(unique = true)
private String username;
@ToString.Exclude
private String password;
@Transient
@JsonProperty("remember_me")
private Boolean rememberMe;
@Enumerated(EnumType.STRING)
@CollectionTable(name = "tbl_user_role")
@ElementCollection(fetch = FetchType.EAGER)
Set<Role> roles = new HashSet<>();
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<ProductEntity> products = new ArrayList<>();
}
@Data
@Entity
@Table(name = "tbl_product")
@EqualsAndHashCode(callSuper = true)
public class ProductEntity extends BaseEntity {
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private UserEntity user;
private String productName;
}
两者都继承自 BaseEntity
:
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Version
private long version;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BaseEntity that = (BaseEntity) o;
return Objects.equals(id, that.id) && Objects.equals(version, that.version);
}
@Override
public int hashCode() {
return Objects.hash(id, version);
}
}
现在,当我尝试检索所有产品(或所有用户)(例如 findAll
方法),我会收到 StackOverflowError
错误。
我知道这个错误是由于用户和产品之间的循环依赖引起的,所以我在 UserEntity
的 equals 注解中添加了排除项来解决它,就像这样:
@EqualsAndHashCode(callSuper = true, exclude = "products")
不幸的是,错误仍然不断出现。我在这里漏掉了什么?
英文:
I have two entity classes:
@Data
@Entity(name = "user")
@Table(name = "tbl_user")
@EqualsAndHashCode(callSuper = true, exclude = "products")
public class UserEntity extends BaseEntity {
@Column(unique = true)
private String username;
@ToString.Exclude
private String password;
@Transient
@JsonProperty("remember_me")
private Boolean rememberMe;
@Enumerated(EnumType.STRING)
@CollectionTable(name = "tbl_user_role")
@ElementCollection(fetch = FetchType.EAGER)
Set<Role> roles = new HashSet<>();
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<ProductEntity> products = new ArrayList<>();
}
@Data
@Entity
@Table(name = "tbl_product")
@EqualsAndHashCode(callSuper = true)
public class ProductEntity extends BaseEntity {
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private UserEntity user;
private String productName;
}
both extending a baseEntity:
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Version
private long version;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BaseEntity that = (BaseEntity) o;
return Objects.equals(id, that.id) && Objects.equals(version, that.version);
}
@Override
public int hashCode() {
return Objects.hash(id, version);
}
}
Now when I try to retrieve all products (or all users) (e.g. findAll
method) I get a StackOverflowError
.
I know this error is caused by the circular dependency between user and products, so I added an exclude to the equals annotation in the userEntity to resolve it, like so:<br><br> @EqualsAndHashCode(callSuper = true, exclude = "products")
Unfortunately the error keeps popping up. What am I missing here?
答案1
得分: 1
如果有帮助的话,Lombok还有一个很有用的注解@EqualsAndHashCode.Exclude
,非常适用于测试!
英文:
In case it might help, lombok also has anotation @EqualsAndHashCode.Exclude
very useful for tests!
答案2
得分: 0
我通过在roles
和user
类变量上添加@ToString.Exclude
来解决了这个问题。显然后来在代码的某个地方我打印了它们,这导致了错误。
英文:
I managed to fix the issue by adding @ToString.Exclude
to the roles
and user
class variables. Apparently I was printing them somewhere later in the code, which was causing the error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论