0

This is my code in Dao session = sessionFactory.openSession(); tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Router.class); criteria.add(Restrictions.eq("router_id", router_id));
Router router = (Router) criteria.uniqueResult();
tx.commit(); session.close(); return router;

In Hibernate getting like this {"org.hibernate.NonUniqueResultException: query did not return a unique result: 4"}

Thanks in advance

BMAM
  • 73
  • 1
  • 2
  • 9
  • You can update a timestamp like [this](https://stackoverflow.com/questions/40969919/hibernate-5-auto-updated-timestamp-field-for-last-modified) or if you already have a field with the timestamp, you need to modify your query. – awagenhoffer Dec 22 '17 at 07:39
  • Possible duplicate of [org.hibernate.NonUniqueResultException: query did not return a unique result: 2?](https://stackoverflow.com/questions/23866709/org-hibernate-nonuniqueresultexception-query-did-not-return-a-unique-result-2) – GROX13 Jan 31 '19 at 08:32

1 Answers1

1

According to official hibernate doc,

Thrown when the application calls Query.uniqueResult() and the query returned more than one result.

In your case, 4 results are returned. Make sure your query only returns one result.

Minjun Yu
  • 3,497
  • 4
  • 24
  • 39
  • Yes, i`m getting multiple results. But i need only last updated result. – BMAM Dec 22 '17 at 06:48
  • How can i get last updated result? Thanks in advance – BMAM Dec 22 '17 at 06:49
  • use setMaxResults to get 1 result. @BMAM – Ajay Garg Dec 22 '17 at 12:20
  • @BMAM there are multiple ways to get last updated result like get the first result by the last updated time in your table (assuming you have one, it is pretty easy to do with HQL or criteria). you can create a new thread to get help on this question. – Minjun Yu Dec 22 '17 at 16:51