首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring -如何使用@Embedded属性进行JpaRepository查询?

Spring -如何使用@Embedded属性进行JpaRepository查询?
EN

Stack Overflow用户
提问于 2022-11-15 12:33:45
回答 1查看 27关注 0票数 0

我试图使用来自嵌入式类的属性进行existsBy查询,但我正在接收"No property 'cpf' found for type 'Patient'"

类病人使用Person类作为嵌入。

Person.java

代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
@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 (部分)

代码语言:javascript
运行
复制
@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 (部分)

代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
@Repository
public interface PatientRepository extends JpaRepository<Patient, UUID> {

    boolean existsByCpf(String cpf);
}

如何将@Embedded属性传递给@Repository?

EN

回答 1

Stack Overflow用户

发布于 2022-11-15 14:31:15

您可以尝试使用_嵌入的文件名和它的文件进行分离。

代码语言:javascript
运行
复制
@Repository
public interface PatientRepository extends JpaRepository<Patient, UUID> {

    boolean existsByPerson_Cpf(String cpf);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74445712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档