【发布时间】:2019-03-01 13:54:08
【问题描述】:
我有以下具有双向映射的 JPA 实体。我正在尝试将所有 featureGroup 提取到 DTO 中。
如果我确实在 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。但还是不行。
【问题讨论】:
-
在您的 AppFeatureGroup 中,您有 @OneToMany(mappedBy="appFeature") 修复此问题,重试可能会解决(尚未详细查看)
-
@Mahieus,这是什么错误?
-
抱歉忘记发布正确的注释,但它可能应该是该类中的@OneToMany(mappedBy="appGroupFeature")
-
对不起。我没有在这里复制确切的代码。当我起草时,有一种类型。但是,映射很好。我确定了这个问题。我使用 lombok 使用 @Data 注释。这已经给出了问题。我删除了 lombok 并创建了实际的 getter setter。现在它工作正常。龙目岛毁了我的一天。
标签: java spring-data-jpa hibernate-mapping bidirectional-relation