我正在尝试使用来自嵌入式类的属性进行existsBy
查询,但我正在接收"No property 'cpf' found for type 'Patient'"
。
类Patient使用嵌入的Person类。
位于
@Embeddable
@Data
public class Person {
@Column(nullable = false, length = 11)
private String cpf;
@Column(name = "full_name", nullable = false, length = 60)
private String fullName;
@Column(nullable = false)
private String birthdate;
@Column(name = "email", nullable = true, length = 30)
private String emailAddress;
@Column(name = "cellphone_number", nullable = true, length = 11)
private String cellphoneNumber;
}
Patient.java
@Data
@Entity
@Table(name = "tb_patient")
public class Patient implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "patient_id")
private UUID id;
@Column
private LocalDateTime registrationDate;
@Embedded
private Person Person;
}
PatientController.java (part of)
@PostMapping
public ResponseEntity<Object> savePatient(@RequestBody Person person) {
if(patientService.existsByCpf(person.getCpf())) {
return ResponseEntity.status(HttpStatus.CONFLICT).body("CONFLICT: CPF number is already in use!");
}
var patientModel = new Patient();
BeanUtils.copyProperties(person, patientModel);
patientModel.setRegistrationDate(LocalDateTime.now(ZoneId.of("UTC")));
return ResponseEntity.status(HttpStatus.CREATED).body(patientService.save(patientModel));
}
PatientService.java (part of)
@Service
public class PatientService {
final PatientRepository patientRepository;
public PatientService(PatientRepository patientRepository) {
this.patientRepository = patientRepository;
}
public boolean existsByCpf(String cpf) {
return patientRepository.existsByCpf((cpf));
}
PatientRepository.java
@Repository
public interface PatientRepository extends JpaRepository<Patient, UUID> {
boolean existsByCpf(String cpf);
}
我如何将@Embedded属性传递给@Repository?
您可以尝试通过_
嵌入的文件名和它的文件分开。
@Repository
public interface PatientRepository extends JpaRepository<Patient, UUID> {
boolean existsByPerson_Cpf(String cpf);
}