There is a table in the MySQL database. You need to select one random entry and get an entity object at the output.
I tried to do it like this:
Entity getRandomEntity() { int rand = new Random().nextInt(count); Session session = sessionFactory.getCurrentSession(); return (Entity) session.createQuery("from Entity where id = :rand") .setParameter("rand", rand).getSingleResult(); } But it does not work, because records in the database are sometimes deleted and, accordingly, id have gaps.
What can be done here?