我有以下JPA实体具有双向映射。我正在尝试将所有featureGroup提取到DTO中。
如果我在featureGroup和iteratinng中找到所有内容以获取其功能。它不会来。我还不熟悉JPA。我的方法是否正确?
以下是我的实体。
@Entity
@Table(name="application")
@Data
class Application{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) id;
name;
@OneToMany(mappedBy="application")
private Set<AppFeatureGroup> appFeatureGroup;
}
然后
@Entity
@Table(name="appfeaturegroup")
@Data
class AppFeatureGroup {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) id;
title;
@OneToMany(mappedBy="appfeaturegroup")
private Set<AppFeature> appFeature;
@ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="id", insertable=false, updatable=false)
private Application application;
}
然后
@Entity
@Table(name="appfeature")
@Data
class AppFeature{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) id;
title;
@OneToMany(mappedBy="appFeature")
private Set<AppSubFeature> appSubFeature;
@ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="id", insertable=false, updatable=false)
private AppFeatureGroup appFeatureGroup;
}
和
@Entity
@Table(name="appsubfeature")
@Data
class AppSubFeature{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) id;
title;
@ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="id", insertable=false, updatable=false)
private AppFeature appFeature;
}
然后
我试着得到如下的对象:
List<AppFeatureGroup> appFeatureGroupList = appFeatureGroupRepository.finAll()
//Also tried from Application application = findById(id) and from application also I tried to get the deep objects
for(AppFeatureGroup appFeatureGroup : appFeatureGroupList){
//I get id and title. But,
Set<AppFeature> appFeature = appFeatureGroup.getAppFeature();//This is empty
}
我实施的是不正确的?我也试过fetch=FethType.EAGER
。但仍然没有工作。
我从lombok删除了@Data。现在它的工作正常。因为这个lombok我得到如下错误:
WARN [org.hibernate.engine.loading.internal.LoadContexts] (default task-1) HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext
而另一个错误是,
jpa Exception occurred: com.sun.jdi.InvocationException occurred invoking method..
当有多个映射时,不要将@Data
用于Entity
。
这可能是因为使用了lombok生成的equals,hashcode和toString方法(通过@Data注释),其中包含实体之间的双向链接。因此调用它们可能会产生类似StackOverflowException和其他许多异常。
用@ Setter,@ Getter,@ EqualsAndHashcode(exclude = {}),@ ToString(exclude = {})替换@Data。