简体   繁体   English

使用Spring Data通过实体ID从实体获取嵌入式对象

[英]Get embedded object from entity by entity id using Spring Data

I have an entity Applicant which contains @Embedded Name object ( firstName , lastName etc.). 我有一个实体Applicant ,其中包含@Embedded Name对象( firstNamelastName等)。 I would like to get only the embedded object from database by applicant id using Spring Data. 我想使用Spring Data通过申请人ID从数据库中仅获取嵌入式对象。

I'm trying like this: 我正在尝试这样:

public interface ApplicantRepository extends Repository<Applicant, UUID> {
    Optional<Name> findNameById(final UUID applicantId);
}

but it returns the whole Applicant entity instead of just Name. 但它会返回整个申请人实体,而不仅仅是名称。 Is there any option to name the function in different way to achieve this with Spring Data? 是否有其他选项可以使用Spring Data以不同的方式命名函数?

You can do this by using Query Annotation in Spring-data. 您可以通过在Spring数据中使用查询注释来做到这一点。

    @Query("select applicant.name from Applicant applicant where applicant.id = :id")
    Optional<Name> findApplicantNameById(@Param("id") Integer id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM