The Criteria.uniqueResult()
method make it easier to query a single instance of a persistence object. When no persistence found this method will return a null
value.
package org.kodejava.hibernate;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.kodejava.hibernate.model.Genre;
public class UniqueResultExample {
public static Session getSession() throws HibernateException {
String cfg = "hibernate.cfg.xml";
SessionFactory sessionFactory = new Configuration().configure(cfg)
.buildSessionFactory();
return sessionFactory.openSession();
}
public static void main(String[] args) {
try (Session session = getSession()) {
Criteria criteria = session.createCriteria(Genre.class)
.add(Restrictions.eq("id", 1L));
// Convenience method to return a single instance that matches
// the query, or null if the query returns no results.
Object result = criteria.uniqueResult();
if (result != null) {
Genre genre = (Genre) result;
System.out.println("Genre = " + genre.getName());
}
}
}
}
Maven Dependencies
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.9.Final</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
Well, what about NPE? Anyway, thanks for the post.
Hi Igor, Thanks for the correction. A null check has been added to the code snippet.
Does
list()
method in criteria returnnull
? My understanding is it returns an empty list notnull
. Check the following link.Hi Balaji, the example above is about
Criteria.uniqueResult()
method, not thelist()
method. And the JavaDoc of theuniqueResult()
method says it return a single instance that matches the query ornull
if the query returns no result.Hello there, I’m using last version of Hibernate, and it tells me that “The method
createCriteria(Class)
from the typeSharedSessionContract
is deprecate”. What can I do? Thanks a lot dude.Hi Mosab, the
createCriteria(Class persistence)
method of theSharedSessionContract
and othercreateCriteria(...)
methods was deprecated since Hibernate 5.2 and we are suggested to use the JPA Criteria API instead.Thanks for information, I have another question how to get max id in table with hibernate 5 because criteria hiberbate it deprecated.
Thanks.
Be aware that uniqueResult() throws an exception if the resultset contains multiple rows so this isn’t a catch-all for getting a single result.
From the documentation (https://docs.jboss.org/hibernate/orm/current/javadocs/org/hibernate/Criteria.html#uniqueResult–)
Throws: HibernateException – if there is more than one matching result