user3663882
user3663882

Reputation: 7357

Random SELECT from PostgreSQL database via Hibernate

I'm using PostgreSQL and what I need to do is to perform a random SELECT query from a certain table in my database (via Hibernate). What would be the best way of achieving this?

Here is my code so far:

criteria.addOrder(Order.asc("RANDOM()"))

where RANDOM() is the PostgreSQL function. However, there is no property with such a name in the Entity class, and therefore, a HibernateException gets thrown.

Upvotes: 1

Views: 206

Answers (1)

Predrag Maric
Predrag Maric

Reputation: 24433

You could implement your own Order class, using instructions from this blog. You would then use it like criteria.addOrder(OrderBySqlFormula.sqlFormula("RANDOM() asc"));

Upvotes: 2

Related Questions