Piotr Pradzynski
Piotr Pradzynski

Reputation: 4535

Get embedded object from entity by entity id using Spring Data

I have an entity Applicant which contains @Embedded Name object (firstName, lastName etc.). I would like to get only the embedded object from database by applicant id using Spring Data.

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?

Upvotes: 0

Views: 2302

Answers (1)

ArunM
ArunM

Reputation: 2314

You can do this by using Query Annotation in Spring-data.

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

Upvotes: 3

Related Questions