如何防止我的多个关系因infinte递归或json编写失败而失效?

ahy6op9u  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(235)

我正在尝试使用hibernate和maven建立双向多人关系,但它总是抛出org.hibernate.lazyinitializationexception:无法初始化代理-无会话或StackOverflower错误。我已经尝试过@jsonidentityinfo,结果maven自动添加了一个“pk”变量,这主要破坏了我的前端@jsonmanagedreference和@jsonbackreference,这导致接收到backreference的实体被完全忽略了@jsonignore,backreference的问题与@jsonview相同,它看起来什么都没有改变,和带有自定义序列化程序的@jsonserialize,这在实现时也会导致前端出现问题。我还尝试自己直接将查询连接到存储库中,但它导致了相同的问题。我真的力不从心了,不知道还能做什么。
节点

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import de.sadgmbh.spring.angular.backenddemo.model.AbstractAuditingEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)

@Entity
@Table(name = "knotenpunkte")
//@JsonSerialize(using = CustomKnotenpunktSerializer)
public class Knotenpunkt extends AbstractAuditingEntity<Long> {
    @NotNull
    @Column(length = 50, unique = true, nullable = false)
    @JsonView(Views.Public.class)
    private int knotennr;
    @NotNull
    @Column(length = 50, unique = true, nullable = false)
    @JsonView(Views.Public.class)
    private String strasse;
    @NotNull
    @JsonView(Views.Public.class)
    private boolean codierung;
    @NotNull
    @JsonView(Views.Public.class)
    private boolean bake;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "knotenpunkt_linie",
            joinColumns = {@JoinColumn(name = "knotenpunkt_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "linie_id", referencedColumnName = "id")})
    @JsonView(Views.Internal.class)
    Set<Linie> linienSet = new HashSet<>();
}

线

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import de.sadgmbh.spring.angular.backenddemo.model.AbstractAuditingEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Entity
@Table(name = "linien")
//@JsonSerialize(using = CustomLineSerializer.class)
public class Linie extends AbstractAuditingEntity<Long> {
    @NotNull
    @Column(length = 50, unique = true, nullable = false)
    @JsonView(Views.Public.class)
    private String linienNR;
    @ManyToMany( mappedBy = "linienSet")
    @JsonView(Views.Internal.class)
    private Set<Knotenpunkt> knotenpunktSet = new HashSet<>();
}

maven生成的接口

export interface Knotenpunkt extends AbstractAuditingEntity<number> {
    id: number;
    knotennr: number;
    strasse: string;
    codierung: boolean;
    bake: boolean;
    linienSet: Linie[];
}

export interface Linie extends AbstractAuditingEntity<number> {
    id: number;
    linienNR: string;
    knotenpunktSet: Knotenpunkt[];
}

export interface Views {
}

export interface Internal extends Public {
}

export interface Public {
}

export interface JsonSerializer<T> extends JsonFormatVisitable {
    unwrappingSerializer: boolean;
    delegatee: JsonSerializer<any>;
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题