1

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.