gpt4 book ai didi

java - JPA双向映射不获取深度映射数据

转载 作者:行者123 更新时间:2023-12-02 10:12:05 26 4
gpt4 key购买 nike

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

如果我在 featureGroup 上 findAll 并迭代以获取其功能。它不会来。我对 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..

当存在多个映射时,不要对 Entity 使用 @Data

关于java - JPA双向映射不获取深度映射数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54946083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com