我是Spring的初學者,所以如果我犯了一些愚蠢的錯誤,我很抱歉。
人.java
@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;
@Embedded
private Address address;
}
牙醫.java
@Data
@Entity
@Table(name = "tb_dentists")
public class Dentist implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "dentist_id")
private UUID id;
@Column
private LocalDateTime registrationDate;
@Column(nullable = false, unique = true, length = 6)
private String croNumber;
@Embedded
private Person person;
}
牙醫控制器.java
@PostMapping
public ResponseEntity<Object> saveDentist(@RequestBody @Valid Dentist dentistDto, Person person) {
if(dentistService.existsByCroNumber(dentistDto.getCroNumber())) {
return ResponseEntity.status(HttpStatus.CONFLICT).body("CONFLICT: CRO number is already in use!");
}
if(dentistService.existsByPerson_Cpf(person.getCpf())) {
return ResponseEntity.status(HttpStatus.CONFLICT).body("CONFLICT: CPF number is already in use!");
}
var dentistModel = new Dentist();
BeanUtils.copyProperties(dentistDto, dentistModel);
dentistModel.setRegistrationDate(LocalDateTime.now(ZoneId.of("UTC")));
return ResponseEntity.status(HttpStatus.CREATED).body(dentistService.save(dentistModel));
}
牙醫服務.java
public boolean existsByCroNumber(String croNumber) {
return dentistRepository.existsByCroNumber((croNumber));
}
public boolean existsByPerson_Cpf(String cpf) {
return dentistRepository.existsByCpf((cpf));
}
}
DentistRepository.java
@Repository
public interface DentistRepository extends JpaRepository<Dentist, UUID> {
boolean existsByCroNumber(String croNumber);
boolean existsByCpf(String cpf);
}
我正在嘗試使用 existsBy 和 Person 的 CPF 列過濾查詢/代碼。Person 嵌入在 Dentist 物體中。如何正確實作此代碼?我正在嘗試下面發布的內容,但我已經到了任何地方。
Spring 正在為我回傳此錯誤
引起:org.springframework.data.mapping.PropertyReferenceException:沒有找到型別“牙醫”的屬性“cpf”
我只發布了我的代碼的一部分,查詢 existsByCroNumber 作業正常,API 的其余部分也很好。
uj5u.com熱心網友回復:
你應該將你的 repo 方法命名為 existsByPersonCpf。
@Repository
public interface DentistRepository extends JpaRepository<Dentist, UUID> {
boolean existsByCroNumber(String croNumber);
boolean existsByPersonCpf(String cpf);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/533079.html
標籤:爪哇春天jpa
