I have my DB tables designed like below.
With this structure, using Springboot, I am not able to fetch all child elements of TABLE-1 till TABLE-3
Below are the Entity classes for the same
TABLE-1 (SDKMainCollection.java)
@Entity
@Table(name="sdkmaincollection")
public class SDKMainCollection {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String sdkgroupname;
@JsonIgnore
@OneToMany(mappedBy = "sdkcollectionobj", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<SDKCollection> sdkcollection;
public SDKMainCollection() {
}
public SDKMainCollection(int id,String sdkGroupName) {
this.id= id;
this.sdkgroupname =sdkGroupName;
}
public SDKMainCollection(String sdkGroupName) {
this.sdkgroupname =sdkGroupName;
}
//Getters and Setters
}
TABLE-2 (SDKCollection.java)
@Entity
@Table(name="sdkcollection")
public class SDKCollection {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String sdktitle;
private String sdkid;
private String sdkresourceid;
private String sdkdescription;
private String sdkimageName;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sdkmaincollection_id")
private SDKMainCollection sdkcollectionobj;
@JsonIgnore
@OneToMany(mappedBy = "sdkcolsubobj", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
private Set<Ads> ads;
public SDKCollection() {
}
public SDKCollection(int id, String sdkTitle, String sdkId, String sdkresourceId, String sdkdescription,
String sdkimageName,SDKMainCollection sdkObj) {
this.id = id;
this.sdktitle = sdkTitle;
this.sdkid = sdkId;
this.sdkresourceid = sdkresourceId;
this.sdkdescription = sdkdescription;
this.sdkimageName = sdkimageName;
this.sdkcollectionobj = sdkObj;
}
public SDKCollection(String sdkTitle, String sdkId, String sdkresourceId, String sdkdescription,
String sdkimageName,SDKMainCollection sdkObj) {
this.sdktitle = sdkTitle;
this.sdkid = sdkId;
this.sdkresourceid = sdkresourceId;
this.sdkdescription = sdkdescription;
this.sdkimageName = sdkimageName;
this.sdkcollectionobj = sdkObj;
}
//Getters and Setters
}
TABLE-3 (Ads.java)
@Entity
@Table(name="ads")
public class Ads {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String adtitle;
private String adtag;
private String adtype;
private String imagename;
private String controlid;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sdkcollection_id")
private SDKCollection sdkcolsubobj;
public Ads() {
}
public Ads(int id, String adTitle, String adTag, String adType, String imageName, String controlID,
SDKCollection sdkcollection
) {
this.id = id;
this.adtitle = adTitle;
this.adtag = adTag;
this.adtype = adType;
this.imagename = imageName;
this.controlid = controlID;
this.sdkcolsubobj = sdkcollection;
}
public Ads(String adTitle, String adTag, String adType, String imageName, String controlID
,SDKCollection sdkcollection) {
this.adtitle = adTitle;
this.adtag = adTag;
this.adtype = adType;
this.imagename = imageName;
this.controlid = controlID;
this.sdkcolsubobj = sdkcollection;
}
//Getters and Setters
}
And below is my controller class for fetching elements
@RestController
@RequestMapping("/api/sdk/")
@CrossOrigin
public class GetSDKController {
@Autowired
private SDKMainCollectionRepo sdkMainCollectionRepo;
@Autowired
private SDKCollectionRepo sdkCollectionRepo;
@Autowired
private AdsRepo adsRepo;
@GetMapping("/getAllSDKs")
public List<SDKMainCollection> getAllSDK(){
sdkDataMapper = new SDKDataMapper(sdkMainCollectionRepo,sdkCollectionRepo,adsRepo);
List<SDKMainCollection> totalResp = (List<SDKMainCollection>) sdkMainCollectionRepo.findAll();
System.out.println("getAllSDK = totalResp "+totalResp);
//NOT GETTING WHAT IS EXPECTED
return totalResp;
}
}
Below is my expectation , but I am getting Stackoverflow error
Stackoverflow
Error Logs:
java.lang.StackOverflowError: null
at java.util.AbstractCollection.toString(Unknown Source) ~[na:1.8.0_131]
at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:328) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_131]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_131]
at com.dvservices.models.SDKMainCollection.toString(SDKMainCollection.java:59) ~[classes/:na]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_131]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_131]
at com.dvservices.models.SDKCollection.toString(SDKCollection.java:118) ~[classes/:na]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_131]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_131]
at java.util.AbstractCollection.toString(Unknown Source) ~[na:1.8.0_131]
at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:328) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_131]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_131]
at com.dvservices.models.SDKMainCollection.toString(SDKMainCollection.java:59) ~[classes/:na]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_131]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_131]
at com.dvservices.models.SDKCollection.toString(SDKCollection.java:118) ~[classes/:na]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_131]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_131]
at java.util.AbstractCollection.toString(Unknown Source) ~[na:1.8.0_131]
at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:328) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_131]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_131]
at com.dvservices.models.SDKMainCollection.toString(SDKMainCollection.java:59) ~[classes/:na]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_131]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_131]
at com.dvservices.models.SDKCollection.toString(SDKCollection.java:118) ~[classes/:na]
at java.lang.String.valueOf(Unknown Source) ~[na:1.8.0_131]
at java.lang.StringBuilder.append(Unknown Source) ~[na:1.8.0_131]
at java.util.AbstractCollection.toString(Unknown Source) ~[na:1.8.0_131]
at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:328) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
I guess the problem might be with getting last array and causing Stackoverflow error.
EDIT:
Is this problem due to having both OneToMany
and ManyToOne
in same SDKCollection
class as it comes in middle of the hierarchy?
public class SDKCollection {
........
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sdkmaincollection_id")
private SDKMainCollection sdkcollectionobj;
@JsonIgnore
@OneToMany(mappedBy = "sdkcolsubobj", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
private Set<Ads> ads;