gpt4 book ai didi

java - JPA 双向关系抛出 java.lang.StackOverflowError : null

转载 作者:行者123 更新时间:2023-11-30 10:06:13 27 4
gpt4 key购买 nike

我正在尝试将两个实体与 JPA 相关联,当我运行该项目时不会抛出错误,但是当我请求让我成为社区领袖选民的方法时,我得到下一个异常:

java.lang.StackOverflowError: null
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) ~[gson-2.8.5.jar:na]
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:1018) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) ~[gson-2.8.5.jar:na]
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:1018) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) ~[gson-2.8.5.jar:na]
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:1018) ~[gson-2.8.5.jar:na]

我就像一个循环,我不知道如何解决这个问题。

这是我的类(class):

1)领导实体:

@Entity
@Table(name = "leaders")
public class Leader implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

@NotEmpty
String phone;

@NotEmpty
String name;

@OneToMany( mappedBy = "leader", fetch = FetchType.LAZY,cascade = CascadeType.ALL, orphanRemoval = true)
private List<Voter> voters;



public Leader() {
voters = new ArrayList<>();
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getCelular() {
return phone;
}

public void setCelular(String celular) {
this.phone = celular;
}

public String getNombre() {
return name;
}

public void setNombre(String nombre) {
this.name = nombre;
}

public List<Voter> getVotantes() {
return voters;
}

public void setVotantes(List<Voter> votantes) {
this.voters = votantes;
}

public void addVotante(Voter votante){
voters.add(votante);
}
}

2)选民实体:

@Entity
@Table(name = "voters")
public class Voter implements Serializable {

@Id
String id;

String name;

String phone;

String email;

@Column(name = "electoral_school")
String electoralSchool;

@Temporal(TemporalType.DATE)
Date registrationDate;

@ManyToOne(fetch = FetchType.LAZY)
private Leader leader;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Sector sector;

@PrePersist
public void prePersist() {
registrationDate = new Date();
}


public String getCedula() {
return id;
}

public void setCedula(String cedula) {
this.id = cedula;
}

public String getNombre() {
return name;
}

public void setNombre(String nombre) {
this.name = nombre;
}

public String getTelefono() {
return phone;
}

public void setTelefono(String telefono) {
this.phone = telefono;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getColegioElectoral() {
return electoralSchool;
}

public void setColegioElectoral(String colegioElectoral) {
this.electoralSchool = colegioElectoral;
}

public Leader getDirigente() {
return leader;
}

public void setDirigente(Leader dirigente) {
this.leader = dirigente;
}

public Date getRegistro() {
return registrationDate;
}

public void setRegistro(Timestamp registro) {
this.registrationDate = registro;
}
}

3)领导者 Controller :

@Controller
@RequestMapping("/leader")
public class LeaderController {

@Autowired
IDirigenteService leaderService;

@RequestMapping(value="getVoters/{leader_id}")
@ResponseBody
public String getAll(@PathVariable(value = "leader_id") Long leader_id){
Gson gson = new Gson();
return gson.toJson(leaderService.findById(leader_id).getVotantes());

}

}

我在 import.sql 中用测试数据填充表。

提前致谢。

最佳答案

Hibernate OneToMany 或 JPA 双向关系抛出 java.lang.StackOverflowError: null 是一个循环依赖问题,如果您使用 @Data 复杂注解。

要排除对特定字段的循环依赖或更好,如果不需要 toString(),则使用 @Getter@Setter。好吧,当我遇到上述这些问题时,这些解决方案对我有用。

然后您可以在特定属性上使用 @JsonIgnore

@Getter

@Setter

public class MyClass {

}

关于java - JPA 双向关系抛出 java.lang.StackOverflowError : null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54775257/

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