Search code examples
javajsonhibernatejacksonmany-to-many

How to prevent my manytomany relationship from infinte recursion or JSON writing failure?


I'm trying to make a bidirectional manytomany relationship work with Hibernate and Maven, but it always either throws org.hibernate.LazyInitializationException: could not initialize proxy - no Session or a Stackoverflowerror. I already tried @JSONIdentityInfo ,which resulted in Maven automatically adding a "PK" Variable which disrupted mainly my Frontend, @JSONManagedReference and @JSONBackreference, which caused the entity that received the Backreference to be ignored completly, @JSONIgnore, same issue with the Backreference, @JsonView, which changed seemingly nothing, and @JsonSerialize with a custom serializer, which when implemented also causes problems with the frontend. I also tried wirting the Query myself direclty into the Repository but it caused the same issue. I am really at the end of my abilties and dont know what else to do.

Node

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<>();
}

Line


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<>();
}

Generated Interfaces by 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>;
}

Solution

  • In the end the solution was to add @Transactional to the controller. Hope this might help someone.