避免对休眠的循环依赖
avoid circular dependency on hibernate
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 class 和 MedicalDevice class 之间对休眠的循环依赖?我已经实现了@OneToMany List,它是旧状态,@OneToOne State currentState 指示当前状态。我想让它在列表中不是全部分开,但我的实现导致>
enter image description here
您没有提供错误消息,因此很难确切知道是什么导致了循环依赖,但基本思想是它源于 MedicalDevice
引用了 State
反之亦然,所以相互依赖。
(顺便说一句,您还设置了 MedicalDevice
以拥有一个具有 @OneToMany 关系的 State
列表,以及一个具有 @OneToOne 关系的 State
实例。那并没有真正起作用,要么是@OneToMany,要么是@OneToOne。换句话说,要么 MedicalDevice
有一个 State
,要么有很多 State
,但它没有两者都有。我不知道这是否会导致您的问题)。
无论如何,循环依赖可能是由于调用了 equals/hashcode 或 toString 方法。 MedicalDevice
可能会在其 State
变量上调用这些方法,而后者又会在其 MedicalDevice
变量上调用这些方法,等等
一种可能的解决方法是从 equals/hashcode 和 toString 方法中排除引用。但同样,如果没有错误的详细信息,很难说出确切的问题是什么。
使用 @JsonIgnore
注释在您的州 class 内注释 MedicalAdvice 对象。这样,您可以防止从实体 classes.
之间的关系中无限递归获取数据
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 class 和 MedicalDevice class 之间对休眠的循环依赖?我已经实现了@OneToMany List,它是旧状态,@OneToOne State currentState 指示当前状态。我想让它在列表中不是全部分开,但我的实现导致> enter image description here
您没有提供错误消息,因此很难确切知道是什么导致了循环依赖,但基本思想是它源于 MedicalDevice
引用了 State
反之亦然,所以相互依赖。
(顺便说一句,您还设置了 MedicalDevice
以拥有一个具有 @OneToMany 关系的 State
列表,以及一个具有 @OneToOne 关系的 State
实例。那并没有真正起作用,要么是@OneToMany,要么是@OneToOne。换句话说,要么 MedicalDevice
有一个 State
,要么有很多 State
,但它没有两者都有。我不知道这是否会导致您的问题)。
无论如何,循环依赖可能是由于调用了 equals/hashcode 或 toString 方法。 MedicalDevice
可能会在其 State
变量上调用这些方法,而后者又会在其 MedicalDevice
变量上调用这些方法,等等
一种可能的解决方法是从 equals/hashcode 和 toString 方法中排除引用。但同样,如果没有错误的详细信息,很难说出确切的问题是什么。
使用 @JsonIgnore
注释在您的州 class 内注释 MedicalAdvice 对象。这样,您可以防止从实体 classes.