我有以下实体
注册项目
@Data
@NoArgsConstructor
@Entity
@EntityListeners(RegisteredProgramAuditListener.class)
public class RegisteredProgram extends Auditable<String> {
@OneToMany(mappedBy = "registeredProgram", cascade = CascadeType.ALL)
@JsonBackReference
private List<Trainer> trainerList;
@OneToMany(mappedBy = "registeredProgram", cascade = CascadeType.ALL)
@JsonBackReference
private List<Official> officialList;
}
培训师
@Data
@NoArgsConstructor
@EntityListeners(TrainerAuditListener.class)
@Entity
public class Trainer extends Auditable<String> {
@ManyToOne
@JoinColumn(name = "REGISTERED_PROGRAM_ID", nullable = false)
@JsonManagedReference
private RegisteredProgram registeredProgram;
@Type(type = "yes_no")
private Boolean isDeleted = false;
}
官方
@Data
@NoArgsConstructor
@EntityListeners(OfficialAuditListener.class)
@Entity
public class Official extends Auditable<String> {
@ManyToOne
@JoinColumn(name = "REGISTERED_PROGRAM_ID", nullable = false)
@JsonManagedReference
private RegisteredProgram registeredProgram;
@Type(type = "yes_no")
private Boolean isDeleted = false;
}
基本上,我有与RegisteredProgram
有多对一关系的实体(Trainer-RegisteredProgram,Marial-RegisteredProgram)。现在我有一个通过id获取注册程序的服务,我应该只包含所有的Trainer
和Official
,并使用iskeyfalse。我有下面的服务:
服务项目
@Override
public RegisteredProgramRequestDto getRegisteredProgramDto(Long id) {
RegisteredProgram registeredProgram = registeredProgramRepository.getOne(id);
RegisteredProgramRequestDto registeredProgramRequestDto = programRegistrationMapper
.registeredProgramToRequestDto(registeredProgram);
registeredProgramRequestDto.setOfficialDtoList(
registeredProgramRequestDto.getOfficialDtoList()
.stream()
.filter(officialDto -> !officialDto.getIsDeleted())
.collect(Collectors.toList())
);
registeredProgramRequestDto.setTrainerDtoList(
registeredProgramRequestDto.getTrainerDtoList()
.stream()
.filter(trainerDto -> !trainerDto.getIsDeleted())
.collect(Collectors.toList())
);
return registeredProgramRequestDto;
}
我的问题是,有什么方法可以更有效地提高我的服务?
2条答案
按热度按时间uplii1fm1#
是:仅选择培训师和官员
isDeleted
的查询为false是JPA的一部分。@EntityGraph
也是JPA的一部分,但可以通过spring-data-jpa更容易地完成。这通过JPA用一个查询完成所有事情。
hc2pp10m2#
尝试对子实体使用
@Where(clause = "deleted=false")