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

时间:2019-02-19 21:33:56

标签: java spring hibernate jpa relationship

我正在尝试将两个实体与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]

我不喜欢循环,我不知道如何解决这个问题。

这是我的课程:

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)LeaderController:

@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中。

谢谢。

3 个答案:

答案 0 :(得分:1)

您的LeaderVoter类具有相互引用,因此序列化可以递归进行。

@JsonIgnoreProperties添加到相应字段:

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

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

PS:糟糕,是给杰克逊的,而不是给Gson的。但是想法是一样的-看一下here,如何排除要序列化的字段。

答案 1 :(得分:0)

正确,序列化最终以无限递归尝试对彼此引用的对象进行序列化。

在使用Gson序列化引用的类型时,请尝试使用排除策略。

在以下代码段中,我通知Gson排除Leader类中用ManyToOne注释的字段:

    @RequestMapping(value="getVoters/{leader_id}")
    @ResponseBody
    public String getAll(@PathVariable(value = "leader_id") Long leader_id){
        ExclusionStrategy strategy = new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes field) {
                return field.getDeclaringClass() == Leader.class &&
                        field.getAnnotation(OneToMany.class) != null;
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        };

        Gson gson = new GsonBuilder().disableHtmlEscaping()
            .addSerializationExclusionStrategy(strategy)
            .create();
        return gson.toJson(leaderService.findById(leader_id).getVotantes());
    }

答案 2 :(得分:0)

休眠OneToMany或JPA双向关系引发FROM golang AS build-stage RUN go get -u github.com/LK4D4/vndr FROM scratch AS export-stage COPY --from=build-stage /go/bin/vndr / 是循环依赖关系问题,如果您使用java.lang.StackOverflowError: null复杂注释,则可能源自Lombok的toString()自动生成方法。

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

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

@JsonIgnore