简体   繁体   中英

JPA bidirectional mapping not fetching deep mapped data

I have the following JPA entities with bidirectional mapping. I'm trying to fetch all the featureGroup into a DTO.

If I do findAll at featureGroup and iteratinng to get its features. Its not coming. I'm not yet more familiar with JPA. Is my approach is correct?

Below are my entities.

@Entity
@Table(name="application")
@Data
class Application{
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY) id;
    name;
    @OneToMany(mappedBy="application") 
    private Set<AppFeatureGroup> appFeatureGroup;
}

then

@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;
}

then

@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;
}

and

@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;
}

then

I try to get the objects like below:

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    
}

Is it not correct what I implemented is? I tried with fetch=FethType.EAGER also. But still not working.

I have removed @Data from lombok. Now its working fine. Because of this lombok i got error like below:

WARN  [org.hibernate.engine.loading.internal.LoadContexts] (default task-1) HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext

and anoter error is like,

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

Don't use @Data for Entity when there are multiple mappings.

That's probably because of using lombok's generated equals, hashcode and toString methods (by @Data annotation) , which contain bidirectional links between entities. So calling them could produce exceptions like StackOverflowException and many others.

Replace @Data with @Setter, @Getter, @EqualsAndHashcode(exclude = {}), @ToString(exclude ={}).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM