避免对休眠的循环依赖

时间:2021-01-26 18:02:03

标签: java spring hibernate spring-data-jpa spring-data

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 类之间对休眠的循环依赖?我已经实现了@OneToMany List,它是旧状态,@OneToOne State currentState 表示当前状态。我希望它不是在列表中全部分开,但我的实现导致> enter image description here

2 个答案:

答案 0 :(得分:0)

您没有提供错误消息,因此很难确切知道是什么导致了循环依赖,但基本思想是它源于 MedicalDeviceState 的引用和反之亦然,所以彼此依赖。

(顺便说一句,您还设置了 MedicalDevice 以拥有一个 State 的列表与 @OneToMany 关系,以及一个 State 的实例与 @OneToOne 关系。那不是不是真的有效,要么是@OneToMany 要么是@OneToOne。换句话说,要么MedicalDevice 有一个State,要么有很多State,但两者都没有。我不知道这是否会导致您的问题)。

无论如何,循环依赖可能是由于调用了 equals/hashcode 或 toString 方法。 MedicalDevice 可能会在其 State 变量上调用这些方法,而后者又会在其 MedicalDevice 变量上调用该方法,等等。

一个可能的解决方案是从 equals/hashcode 和 toString 方法中排除引用。但同样,如果没有错误的详细信息,就很难说出确切的问题是什么。

答案 1 :(得分:0)

使用 @JsonIgnore 注释在 State 类中注释 MedicalAdvice 对象。这样,您可以防止从实体类之间的关系中无限递归获取数据。

相关问题