Tiina
Tiina

Reputation: 4785

Spring data named query select entity where two columns are equal

I would like to select all Authoritys where Authority.id and Authority.top are equal. While the following could not produce the desired outcome, how to implement it using a named query but not a native one?

@Query("SELECT Authority WHERE Authority.id = Authority.top")  // how?
List<Authority> findTop();

Upvotes: 0

Views: 316

Answers (2)

Chethan Kumar N
Chethan Kumar N

Reputation: 19

I think the query is syntactically wrong.

It has to be,

SELECT * FROM Authority WHERE Authority.id = Authority.top

OR you can query the columns of your interest like this,

SELECT Authority.id, Authority.name FROM Authority WHERE Authority.id = Authority.top

Upvotes: 0

Maciej Kowalski
Maciej Kowalski

Reputation: 26522

Either:

1) Without an alias:

@Query("FROM Authority WHERE id = top")  
List<Authority> findTop();

2) With an alias:

@Query("SELECT a FROM Authority a WHERE a.id = a.top") 
List<Authority> findTop();

In your case option one is preferrable of course

Upvotes: 1

Related Questions