【问题标题】:avoid circular dependency on hibernate避免对休眠的循环依赖
【发布时间】:2021-01-26 18:02:03
【问题描述】:
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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您没有提供错误消息,因此很难确切知道导致循环依赖的原因,但基本思想是它源于MedicalDevice 引用State 的事实,反之亦然,所以互相依赖。

      (顺便说一句,您还设置了 MedicalDevice 以拥有具有 @OneToMany 关系的 State 的列表,以及具有 @OneToOne 关系的 State 的一个实例。这实际上不起作用,要么一个@OneToMany 或者它是@OneToOne。换句话说,MedicalDevice 有一个State 或者它有很多States,但它没有两者兼有。我不知道这是否会导致您的问题)。

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

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

      【讨论】:

      • 没有错误。它工作正常,但是当我调用 api 时,我得到了这个通知
      猜你喜欢
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 2018-08-24
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      相关资源
      最近更新 更多