Issue
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;
}
how to avoid circular dependency on hibernate between State class and MedicalDevice class? I have implemented @OneToMany List which is the old states and @OneToOne State currentState which indicates the current state. I would like to have it separate not all in the list but my implementation causes that> enter image description here
Solution
Annotate MedicalAdvice object inside your State class with @JsonIgnore
annotation. This way, you can prevent infinite recursive fetching of data from relationships between entity classes.
Answered By - DarkMode
Answer Checked By - Katrina (JavaFixing Volunteer)