0

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.

Shakthi
  • 826
  • 3
  • 15
  • 33
  • In your AppFeatureGroup you have @OneToMany(mappedBy="appFeature") fix this a try again might solve it (didn't look at it into detail yet) – mahieus Mar 01 '19 at 15:15
  • @Mahieus, What is the mistake in this? – Shakthi Mar 01 '19 at 15:40
  • Sorry forgot to post to correct annotation but it should probably be @OneToMany(mappedBy="appGroupFeature") in that class – mahieus Mar 01 '19 at 16:20
  • Sorry. I have not copied the exact code here. While I draft, there is a type. But, mapping are fine. I identified the issue. I used @Data annotation using lombok. That has given the issue. I removed the lombok and created actual getter setter. Now its working fine. lombok ruined my day. – Shakthi Mar 01 '19 at 16:32

3 Answers3

1

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.

Shakthi
  • 826
  • 3
  • 15
  • 33
0

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 ={}).

amseager
  • 5,795
  • 4
  • 24
  • 47
0

Or, if you are not using Lombok but faced this issue, exclude parent property in child entity from equals/hashcode/toString.

  • 1
    Could you provide some explanation for your answer? – Subrato Pattanaik Oct 13 '20 at 12:58
  • The short answer is that using parent property in equals/hashcode/toString of child entity produced recursive calls of each other. I have found more details at this topic - https://thorben-janssen.com/ultimate-guide-to-implementing-equals-and-hashcode-with-hibernate/ – Volodymyr Taras Oct 14 '20 at 13:36