我有以下具有双向映射的 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
。但仍然无法正常工作。