避免对hibernate的循环依赖

ecbunoof  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(421)
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;

}

如何避免状态类和medicaldevice类之间对hibernate的循环依赖?我已经实现了@onetomany list(旧状态)和@onetoone state currentstate(表示当前状态)。我想有它分开不是所有的清单,但我的实现原因,输入图像描述这里

s6fujrry

s6fujrry1#

在state类中注解medicaladvice对象 @JsonIgnore 注解。这样,就可以防止从实体类之间的关系中无限递归地获取数据。

nwnhqdif

nwnhqdif2#

您没有提供错误消息,因此很难确切地知道是什么导致了循环依赖,但基本思想是,它源于以下事实 MedicalDevice 引用 State 反之亦然,所以彼此依赖。
(顺便说一句,你还有 MedicalDevice 设置为有 State 有着@onetomany的关系,还有 State 有着一种“一对一”的关系。那真的不管用,要么是“一个女人”,要么是“一个女人”。换个Angular 来说也是 MedicalDevice 有一个 State 或者有很多 State s、 但它并没有两者兼而有之。我不知道这是不是导致你的问题)。
在任何情况下,循环依赖都有可能是由于被调用的equals/hashcode或tostring方法造成的。 MedicalDevice 也许在它的网站上调用这些方法 State 变量,该变量反过来将调用其上的方法 MedicalDevice 变量等。
一个可能的解决方案是从equals/hashcode和tostring方法中排除引用。但同样,如果没有错误的细节,很难判断到底是什么问题。

相关问题