After completing my Hibernate Post, I want to know what will happen if there are more than one row expected when we use uniqueResult method.
So in the same example, I put one more method called getPatientByLastName, where we can pass any last name and print the other details of the patient.
Here is the complete code for Test.Java as per Part 4
package mypack;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.HibernateException;
public class Test {
private static SessionFactory factory;
private static ServiceRegistry serviceRegistry;
public static void main(String[] args) {
/*
createRecords();
getAllRecords();
getPatientByID(1);
getPatientByID(13);
*/
getPatientByLastName("Smith");
}
public static void createRecords() {
try {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
System.out.println("**Example : Hibernate 4 SessionFactory**");
System.out.println("----------------------------------------");
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
patient pt = new patient();
pt.setFirstName("John12");
pt.setLastName("Smith23");
session.saveOrUpdate(pt);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public static void getAllRecords() {
try {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
System.out.println("**Example : Hibernate SessionFactory**");
System.out.println("----------------------------------------");
List allpatients;
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query q1 = session.createQuery("from patient");
allpatients = q1.list();
for (int i =0; i < allpatients.size(); i++)
{
patient pt = (patient) allpatients.get(i);
System.out.println(pt.getLastName());
}
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public static void getPatientByID(int anyvalue) {
try {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
System.out.println("**Example : Hibernate SessionFactory**");
System.out.println("----------------------------------------");
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query q1 = session.createQuery("from patient where id =:idvalue");
q1.setInteger("idvalue", anyvalue);
Object queryResult = q1.uniqueResult();
patient p1 = (patient)queryResult;
tx.commit();
System.out.print(p1.getLastName());
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public static void getPatientByLastName(String anyvalue) {
try {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
System.out.println("**Example : Hibernate SessionFactory**");
System.out.println("----------------------------------------");
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query q1 = session.createQuery("from patient where lastname =:varLastName");
q1.setString("varLastName",anyvalue);
Object queryResult = q1.uniqueResult();
patient p1 = (patient)queryResult;
tx.commit();
System.out.print(p1.getLastName());
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
If we run this, we will get the following error without any problem. Thank god, at least hibernate throwing exception if something is wrong.
**Example : Hibernate SessionFactory**
----------------------------------------
Hibernate: select patient0_.ID as ID0_, patient0_.firstname as firstname0_, patient0_.lastname as lastname0_ from patient patient0_ where lastname=?
org.hibernate.NonUniqueResultException: query did not return a unique result: 2
at org.hibernate.internal.AbstractQueryImpl.uniqueElement(AbstractQueryImpl.java:899)
at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:890)
at mypack.Test.getPatientByLastName(Test.java:162)
at mypack.Test.main(Test.java:26)
0 comments:
Post a Comment