JPA双向映射未获取深映射数据



i具有以下具有双向映射的JPA实体。我正在尝试将所有功能组获取到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

和anoter错误就像,

jpa Exception occurred: com.sun.jdi.InvocationException occurred invoking method..

当有多个映射时,请勿将@Data用于Entity

这可能是因为使用了Lombok生成的平等,hashcode和Tostring方法(通过@Data注释),该方法包含实体之间的双向链接。因此,打电话给他们可以产生诸如StackoverFlowException和其他许多其他例外。

用@setter,@getter,@equalsandhashcode(dublude = {}),@tostring(ublude = {})。

或,如果您不使用Lombok,但面对此问题,请将子体内的父属性排除在Equals/HashCode/tostring中。

相关内容

  • 没有找到相关文章

最新更新