0

我有以下具有双向映射的 JPA 实体。我正在尝试将所有 featureGroup 提取到 DTO 中。

如果我确实 findAll 在 featureGroup 并迭代以获取其功能。它不来。我对 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。但仍然无法正常工作。

4

3 回答 3

1

我已经从 lombok 中删除了@Data。现在它工作正常。由于这个龙目岛,我得到了如下错误:

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..

当有多个映射时不要使用@Datafor 。Entity

于 2019-03-01T16:39:15.290 回答
0

或者,如果您没有使用 Lombok 但遇到此问题,请从 equals/hashcode/toString 中排除子实体中的父属性。

于 2020-10-13T12:44:36.727 回答
0

这可能是因为使用了 lombok 生成的 equals、hashcode 和 toString 方法(通过 @Data 注释),它们包含实体之间的双向链接。所以调用它们可能会产生像 StackOverflowException 和许多其他异常。

将@Data 替换为@Setter、@Getter、@EqualsAndHashcode(exclude = {})、@ToString(exclude ={})。

于 2019-03-01T16:43:01.057 回答