gpt4 book ai didi

java - Spring Boot get请求未使用@OneToMany获取第三级对象

转载 作者:行者123 更新时间:2023-11-30 05:41:54 25 4
gpt4 key购买 nike

我的数据库表设计如下。

使用这种结构,使用 Springboot,我无法获取 TABLE-1 到 TABLE-3 的所有子元素

enter image description here

下面是相同的实体类

表-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
}

表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
}

表 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
}

下面是我用于获取元素的 Controller 类

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

以下是我的期望,但我收到 Stackoverflow 错误

enter image description here

Stackoverflow 错误日志:

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]

我猜问题可能是获取最后一个数组并导致 Stackoverflow 错误。

编辑:

此问题是否是由于位于层次结构中间的同一个 SDKCollection 类中同时存在 OneToManyManyToOne 造成的?

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;

最佳答案

您缺少 @JsonBackReference 注释,这就是您收到 stackoverflow 错误的原因。@JsonIgnore 并不是为了解决无限递归问题而设计的,它只是忽略被序列化或反序列化的带注释的属性。

添加位置

  1. 在 SDKCollection.java 中私有(private) SDKMainCollection sdkcollectionobj;对 SDKMaincollection 的反向引用,
  2. 在 Ads.java 中私有(private) SDKCollection sdkcolsubobj 对 SDKCollection 的反向引用

上面两者都应该用@JsonBackReference注释希望有帮助。

关于java - Spring Boot get请求未使用@OneToMany获取第三级对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55489979/

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