我试图使双向多关系与 Hibernate 和 Maven 一起工作,但它总是抛出 org.hibernate.LazyInitializationException: 无法初始化代理 - 无会话或 Stackoverflow 错误。我已经尝试过@JSONIdentityInfo,这导致Maven自动添加一个“PK”变量,这主要破坏了我的前端,@JSONManagedReference和@JSONBackreference,这导致接收反向引用的实体被完全忽略,@JSONIgnore,与反向引用相同的问题,@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>;
}
最终的解决方案是在控制器中添加
@Transactional
。希望这可以帮助某人。
就我而言,我正在做: @JsonManagedRefence 和 @JsonBackReference,这是错误的,因为这些仅用于父子关系。
对于多对多使用: @JsonIdentityInfo(生成器= ObjectIdGenerators.PropertyGenerator.class,属性=“id”) 希望这对某人有帮助。