1

I'm not sure if I hit the boundaries of the method query with this task. I have two tables, a User table and a Password table. A User may have multiple Passwords (Where as the newest one is the current).

I would like to make a method query in my User repository to fetch a "current" Password for a user name.

In SQL it would look something like this

"Select FIRST(*) from Passwords INNERJOIN Users ON Users.id=Passwords.userId WHERE Users.username=%s ORDER BY Passwords.created DESC"

What I have tried as method query is findFirstPasswordOrderByPasswords_CreatedByUsers_Username(String username)

which gives me an exception:

No property byUsers found for type long! Traversed path: UsersEntity.passwords.created.

I don't know why it is still pointing to the created field. Is this too much for method queries? Or am I doing it simply wrong?

Herr Derb
  • 4,977
  • 5
  • 34
  • 62

1 Answers1

1

You should define this method in Password repository as you are searching for password. In that repository this method will look like:

Passwords findFirstByUser_UsernameOrderByCreatedDesc(String username);

To implement this method, your Password entity class should have reference to Users table on User property(from the query)

  • Does this mean, that I'm limited to only get `UserEntities` from my `UserRepository`? – Herr Derb Oct 12 '17 at 06:13
  • 1
    Yes, query methods in repositories are limited to entity, which repository is connected to. However, you can use method with custom @Query annotation containing query you want. – Aleksandr Primak Oct 12 '17 at 15:26