
在spring data jpa应用中,我们经常使用@embedded注解将一个实体(如person)作为值类型嵌入到另一个实体(如dentist)中。这种设计有助于提高领域模型的内聚性,并将相关属性组织在一起。然而,当尝试直接通过父实体仓库查询嵌入式实体的内部属性时,初学者可能会遇到propertyreferenceexception,因为spring data jpa的查询解析器需要特定的命名约定来识别这些嵌套属性。
考虑以下实体结构:
Person.java (嵌入式实体)
@Embeddable
@Data
public class Person {
private String cpf; // 身份证号
private String fullName; // 姓名
// ... 其他属性
}Dentist.java (主实体)
@Data
@Entity
@Table(name = "tb_dentists")
public class Dentist implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
@Embedded // 嵌入Person实体
private Person person;
private String croNumber; // 注册号
// ... 其他属性
}假设我们想在DentistRepository中查询是否存在某个特定CPF的牙医。如果尝试这样定义仓库方法:
DentistRepository.java (错误示例)
@Repository
public interface DentistRepository extends JpaRepository<Dentist, UUID> {
boolean existsByCroNumber(String croNumber); // 工作正常
// 错误尝试:直接引用嵌入式实体内部属性名
boolean existsByCpf(String cpf); // 会抛出 PropertyReferenceException
}当调用existsByCpf(String cpf)时,Spring Data JPA会在Dentist实体中查找名为cpf的属性。由于Dentist实体本身并没有cpf属性,cpf是Person实体内部的属性,Spring Data JPA无法直接识别,从而抛出org.springframework.data.mapping.PropertyReferenceException: No property 'cpf' found for type 'Dentist'异常。
Spring Data JPA针对嵌入式实体的查询方法有一套明确的命名约定。要查询嵌入式实体内部的属性,需要将嵌入式实体在父实体中的字段名与嵌入式实体内部的属性名通过下划线或驼峰式连接起来。
在我们的例子中,Person实体在Dentist实体中的字段名为person,Person实体内部的属性名为cpf。因此,正确的查询方法命名应该是existsByPersonCpf。
DentistRepository.java (正确示例)
@Repository
public interface DentistRepository extends JpaRepository<Dentist, UUID> {
boolean existsByCroNumber(String croNumber);
// 正确的命名方式:<嵌入式实体字段名><嵌入式实体内部属性名>
boolean existsByPersonCpf(String cpf);
}现在,我们可以在服务层和控制器层安全地使用这个查询方法:
DentistService.java
@Service
public class DentistService {
private final DentistRepository dentistRepository;
public DentistService(DentistRepository dentistRepository) {
this.dentistRepository = dentistRepository;
}
public boolean existsByCroNumber(String croNumber) {
return dentistRepository.existsByCroNumber(croNumber);
}
public boolean existsByPersonCpf(String cpf) {
// 调用正确的仓库方法
return dentistRepository.existsByPersonCpf(cpf);
}
public Dentist save(Dentist dentist) {
// ... 保存逻辑
return dentistRepository.save(dentist);
}
}DentistController.java
@RestController
@RequestMapping("/dentists")
public class DentistController {
private final DentistService dentistService;
public DentistController(DentistService dentistService) {
this.dentistService = dentistService;
}
@PostMapping
public ResponseEntity<Object> saveDentist(@RequestBody @Valid Dentist dentistDto) {
// 检查CRO号是否已存在
if (dentistService.existsByCroNumber(dentistDto.getCroNumber())) {
return ResponseEntity.status(HttpStatus.CONFLICT).body("CONFLICT: CRO number is already in use!");
}
// 检查CPF是否已存在,注意这里从 dentistDto.getPerson().getCpf() 获取CPF
if (dentistService.existsByPersonCpf(dentistDto.getPerson().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));
}
}在DentistController中,当接收Dentist对象时,其内部的Person对象及其cpf属性已经通过请求体绑定。因此,我们可以直接通过dentistDto.getPerson().getCpf()来获取CPF值进行查询。
通过遵循Spring Data JPA的命名约定,即在查询方法中将嵌入式实体在父实体中的字段名与嵌入式实体内部的属性名连接起来(如existsByPersonCpf),我们可以轻松地对嵌入式实体的属性进行查询,从而避免PropertyReferenceException并构建健壮的数据访问层。理解并正确应用这一机制是有效利用Spring Data JPA处理复杂领域模型关系的关键。
以上就是Spring Data JPA 中嵌入式实体查询方法的正确姿势的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号