Home > Enterprise >  Spring Data JPA - How can I make a existsBy query using an embedded entity?
Spring Data JPA - How can I make a existsBy query using an embedded entity?

Time:11-14

I'm a beginner with Spring, so I'm sorry if I make some dumb mistake.

Person.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;

}

Dentist.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;

}

DentistController.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));
    }

DentistService.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);
}

I'm trying to filter the query/code using existsBy and the CPF column of Person. Person is embedded in the Dentist entity. How can I can properly implement this code? I'm trying this as posted below but I'm getting anywhere.

Spring is returning this error for me

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property 'cpf' found for type 'Dentist'

I posted just some part of my code, the query existsByCroNumber is working properly and the rest of the API is good too.

CodePudding user response:

you should name your repo method existsByPersonCpf.

@Repository
public interface DentistRepository extends JpaRepository<Dentist, UUID> {
  boolean existsByCroNumber(String croNumber);

  boolean existsByPersonCpf(String cpf);
}
  • Related