-1

I'm using a subselect to select the latest post. Is that a good idea?

@Query("SELECT p FROM Post p INNER JOIN p.user WHERE p.user.username = :username AND p.updated = (SELECT MAX(p.updated) FROM Post p WHERE p.user.username = :username)")
Post findLatestByUsername(@Param("username") String username);
user672009
  • 4,379
  • 8
  • 44
  • 77

1 Answers1

-1

I think the best way to select latest row it using index descening

set username, updated columns as descening index if you dont have index on those columnes, for more information how to set it you can look link bellow

https://www.postgresql.org/docs/9.5/static/sql-createindex.html

then your select will be like this

SELECT * FROM Post p where p.user.username = :username LIMIT 1

your reading data will be already descening sortet after select, an then you can select 1 row and it will be latest row, data was decening sorted by username(and it is in where clause) and updated column

and about limit in HQL, you can solve this problem by other way please look links bellow

https://groups.google.com/forum/#!topic/nhibernate-development/JYE3NDcTUEA

How do you do a limit query in HQL?

LIMIT in Postgres not supported in HQL?

Community
  • 1
  • 1
Vecchiasignora
  • 1,275
  • 7
  • 6