Rocky Inde
Rocky Inde

Reputation: 1531

How to query using fields of subclasses for Spring data repository

Here is my entity class:

public class User {

    @Id
    UserIdentifier userIdentifier;
    String name;
}

public class UserIdentifier {

    String ssn;
    String id;
}

Here is what I am trying to do:

public interface UserRepository extends MongoRepository<User, UserIdentifier>
{
    User findBySsn(String ssn);
}

I get an exception message (runtime) saying:

No property ssn found on User!

How can I implement/declare such a query?

Upvotes: 1

Views: 1676

Answers (1)

Rocky Inde
Rocky Inde

Reputation: 1531

According to Spring Data Repositories reference:

Property expressions can refer only to a direct property of the managed entity, as shown in the preceding example. At query creation time you already make sure that the parsed property is a property of the managed domain class. However, you can also define constraints by traversing nested properties.

So, instead of

User findBySsn(String ssn);

the following worked (in my example):

User findByUserIdentifierSsn(String ssn);

Upvotes: 3

Related Questions