我知道在过去使用spring的双向关系有很多问题,但是我的情况有点不同,因为我使用了3个有两个关系的实体来实现一个医疗系统。
我有三个实体:医生/病人/预约
以下是这三个实体的代码--请注意所有已实现的setter、getter和constructors,但为了清楚起见,请在这里删除
病人类
@Entity
public class resPatient {
@Id
@GeneratedValue( strategy= GenerationType.IDENTITY )
private long code;
private String name;
private String gender;
private String email;
private String mobile;
private int age;
private String notes;
@OneToMany(mappedBy = "patient")
List<resPackageMembership> memberships;
@OneToMany(mappedBy = "patient")
List<resAppointment> appointments;
@OneToMany(fetch = FetchType.LAZY,mappedBy = "patient")
List<resMedImage> medImages;博士级
@Entity
public class resDoctor {
@Id
@GeneratedValue( strategy= GenerationType.IDENTITY )
private long code;
private String name;
private String mobile;
private String email;
private String gender;
private int age;
private String speciality;
@OneToMany(mappedBy = "doctor")
List<resAppointment> appointments;约会类
@Entity
public class resAppointment {
@Id
@GeneratedValue( strategy= GenerationType.IDENTITY )
private long code;
private String speciality;
@Basic
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
@Basic
@Temporal(TemporalType.TIMESTAMP)
private Date dateToVisit;
private String status;
private String notes;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "doctorCode")
private resDoctor doctor;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "patientCode")
private resPatient patient;我的医疗系统的工作方式应该是,当我让一个病人使用我的restful控制器时,我想要所有的病人数据,包括他的预约,但这会导致一个无限循环,因为预约有医生,还有预约等等。
我不能使用@JSONIGNORE,因为有两个关系,我想让病人与他的预约,这应该有医生没有预约数组,不应该有任何病人的数据,因为我已经在病人对象
发布于 2020-12-27 14:15:23
作为一般的最佳实践,建议将实体与用于rest控制器的数据传输对象分开。在DTO就绪之后,您可以更多地控制在其中包含和序列化哪些数据,以避免循环引用。如果您喜欢签出https://bootify.io,它将从数据库模式生成DTO,但是您仍然需要定义/构建自定义端点。
发布于 2021-01-04 18:59:18
我最近开发了一个名为豆刀的注释处理器,它支持从任何类生成DTO。您需要通过注释配置。但你不需要改变原来的课程。此库支持在单独的类上进行配置。当然,您可以选择您想要的财产和不需要的财产。还可以通过config类中的静态方法添加新属性。关于你的问题:
// this will generate a DTO class named "resPatientView".
// You can change this name using genName attribute.
@ViewOf(value=resPatient.class, includePattern = ".*")
public class PatientViewConfigure {
// here tell the processor to automatically convert the property appointments from List<resAppointment> to List<resAppointmentWithoutPatient>.
// resAppointmentWithoutPatient is the generated class configured at the following.
// Note, although at this moment it not exists and your idea think it is an error.
// this code really can be compiled, and after compiled, all will ok.
@OverrideViewProperty("appointments")
private List<resAppointmentWithoutPatient> appointments;
}
// here generated a class named resAppointmentWithoutPatient whick has all properties of resAppointment except patient
@ViewOf(value=resAppointment.class, genName="resAppointmentWithoutPatient", includePattern = ".*", excludes={"patient"})
public class AppointmentWithoutPatientViewConfigure {
// the doctor property will be converted to its dto version which defined by the configure class DoctorWithoutAppointmentsViewConfigure.
@OverrideViewProperty("doctor")
private resDoctorWithoutAppointments doctor;
}
// here we generate a class which has all properties of resDoctor except appointments
@ViewOf(value=resDoctor.class, genName="resDoctorWithoutAppointments", includePattern = ".*", excludes={"appointments"})
public class DoctorWithoutAppointmentsViewConfigure {}
// in you rest controller. return the dto instead of the entities.
resPatient patient = ...
resPatientView dto = resPatientView.read(patient);
List<resPatient> patients = ...
List<resPatientView> dto = resPatientView.read(patients);最后,类resPatientView将与resPatient具有相同的形状,除非它的约会没有病人属性,而且它的医生属性被替换为没有约会属性的版本。
下面是更多的示例。版本1.10已经准备好了。将修复一些bug,并支持在spring之前管理配置bean。
https://stackoverflow.com/questions/65466339
复制相似问题