package com.example.demo.model;
import com.sun.istack.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.util.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name="MEDICAL_DEVICE")
public class MedicalDevice {
public MedicalDevice(UUID deviceId, DefectPriority defectPriority, State currentState) {
this.deviceId = deviceId;
this.defectPriority = defectPriority;
this.currentState = currentState;
}
public MedicalDevice(UUID deviceId, State currentState) {
this.deviceId = deviceId;
this.currentState = currentState;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name="DEVICE_ID")
@NotNull
private UUID deviceId;
@Column(name="DEFECT_PRIORITY", nullable = true)
@Enumerated(EnumType.STRING)
private DefectPriority defectPriority;
@OneToMany(mappedBy="medicalDevice")
private List<State> states = new ArrayList<>();
@OneToOne(mappedBy="medicalDevice")
private State currentState;
}package com.example.demo.model;
import com.sun.istack.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name="STATE")
public class State {
public State(StateNames state, UUID enteredBy, LocalDateTime enteredAt) {
this.state = state;
this.enteredBy = enteredBy;
this.enteredAt = enteredAt;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Enumerated(EnumType.STRING)
private StateNames state;
@Column(name="USER_ID")
private UUID enteredBy; //User who changed the devices state to this one
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="MEDICAL_DEVICE_ID")
@NotNull
private MedicalDevice medicalDevice;
@Column(name = "LOCAL_DATE_TIME", columnDefinition = "TIMESTAMP")
private LocalDateTime enteredAt; //Time when the devices state was changed into this one
@Column(name = "LOCAL_DATE", columnDefinition = "DATE")
private LocalDate availabilityDate; //Date when a device will be available (only used in defect states and bought state)
@OneToMany(mappedBy="state")
private List<AdditionalInformation> additionalInformation;
}如何避免State类和MedicalDevice类之间对hibernate的循环依赖?我已经实现了@OneToMany List和@OneToOne state currentState,前者是旧状态,后者表示当前状态。我希望将它们分开,不是列表中的全部,而是我的实现会导致that> enter image description here
发布于 2021-01-28 17:37:39
使用@JsonIgnore批注在State类中批注MedicalAdvice对象。这样,您就可以防止从实体类之间的关系中无限递归地获取数据。
发布于 2021-01-27 02:15:13
您没有提供错误消息,因此很难确切知道是什么导致了循环依赖,但基本思想是,它源于MedicalDevice对State的引用,反之亦然,因此两者相互依赖。
(顺便说一下,您还将MedicalDevice设置为具有@OneToMany关系的State列表,以及具有@OneToOne关系的State实例。这并不是真的有效,要么是@OneToMany,要么是@OneToOne。换句话说,MedicalDevice要么只有一个State,要么有多个State,但并不是两者都有。我不知道这是否导致了您的问题)。
在任何情况下,循环依赖都可能是由于被调用的equals/hashcode或toString方法造成的。MedicalDevice可能会在其State变量上调用这些方法,而后者将在其MedicalDevice变量上调用该方法,依此类推。
一种可能的解决方案是排除equals/hashcode和toString方法中的引用。但是再说一次,如果没有错误的细节,很难说出确切的问题是什么。
https://stackoverflow.com/questions/65906757
复制相似问题