hi,
The "HibernateUtil" class is written by you i mean developer. It is only given as a sample from
www.hibernate.org!
So, you must write a singleton pattern supported class and it can have different methods for each core process i mean select, delete, insert, update.
This is a sample class for you: [from my project]
public final class HibernateUtil {
// [SINGLETON] constructor...
private HibernateUtil() {
try {
// Create the SessionFactory from hibernate.cfg.xml
System.out.println("Creating the SessionFactory from hibernate.cfg.xml");
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
System.out.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
// Instance returner...
public static HibernateUtil getTheInstance() {
if (theInstance == null) {
theInstance = new HibernateUtil();
}
return theInstance;
}
public static SessionFactory getSessionFactory() {
System.out.println("Session factory will be returned!");
return sessionFactory;
}
// [INSERTION] method!
public SonucMesaj insert(Object object) {
/**/
System.out.println("Insert...");
/**/
initializeSession();
//HibernateUtil.setCastedObject(object);
/**/
System.out.println("Eklenecek nesne : " + object);
/**/
theSession.save(object);
theSession.flush();
finalizeSession(theSession);
if (jobFlag) {
System.out.println(object.toString() + " is inserted!");
sonucMesaj.setSonucMesajString("Kayýt ekleme iþlemi baþarýlý.Sorgu sonuç listesi :");
sonucMesaj.setSonucTip(Constants.MESSAGE_SUCCESS);
// Refreshing...
sonucMesaj.setSonucListe(getRefreshedList(object));
} else {
sonucMesaj.setSonucMesajString("Kayýt ekleme iþlemi baþarýsýz!Lütfen yeniden deneyiniz...");
sonucMesaj.setSonucTip(Constants.MESSAGE_FAILURE);
}
return sonucMesaj;
}
// [UPDATE] method!
public SonucMesaj update(Object object) {
/**/
System.out.println("Update...");
/**/
initializeSession();
//HibernateUtil.setCastedObject(object);
theSession.update(object);
theSession.flush();
finalizeSession(theSession);
if (jobFlag) {
System.out.println(object.toString() + " is updated!");
sonucMesaj.setSonucMesajString("Kayýt güncelleme iþlemi baþarýlý.Sorgu sonuç listesi :");
sonucMesaj.setSonucTip(Constants.MESSAGE_SUCCESS);
// Refreshing...
sonucMesaj.setSonucListe(getRefreshedList(object));
} else {
sonucMesaj.setSonucMesajString("Kayýt güncelleme iþlemi baþarýsýz!Lütfen yeniden deneyiniz...");
sonucMesaj.setSonucTip(Constants.MESSAGE_FAILURE);
}
return sonucMesaj;
}
// [DELETE] method!
public SonucMesaj delete(Object object) {
/**/
System.out.println("Delete...");
/**/
initializeSession();
//HibernateUtil.setCastedObject(object);
theSession.delete(object);
theSession.flush();
finalizeSession(theSession);
if (jobFlag) {
System.out.println(object.toString() + " is deleted!");
sonucMesaj.setSonucMesajString("Kayýt silme iþlemi baþarýlý.Sorgu sonuç litesi :");
sonucMesaj.setSonucTip(Constants.MESSAGE_SUCCESS);
// Refreshing...
sonucMesaj.setSonucListe(getRefreshedList(object));
} else {
sonucMesaj.setSonucMesajString("Kayýt silme iþlemi baþarýsýz!Lütfen yeniden deneyiniz...");
sonucMesaj.setSonucTip(Constants.MESSAGE_FAILURE);
}
return sonucMesaj;
}
// [SELECT] method!
public SonucMesaj select(Object object) {
/**/
System.out.println("Select...");
/**/
initializeSession();
/**/
System.out.println(object);
/**/
List list = theSession.createQuery("from " + object.getClass().getName()).list();
/**/
System.out.println("Liste durum : " + list.isEmpty());
/**/
theSession.flush();
finalizeSession(theSession);
System.out.println("Selection is okay!");
if (jobFlag) {
sonucMesaj.setSonucMesajString("Veri çekme iþlemi baþarýlý.Sorgu sonuç listesi :");
sonucMesaj.setSonucListe(list);
sonucMesaj.setSonucTip(Constants.MESSAGE_SUCCESS);
} else {
sonucMesaj.setSonucMesajString("Veri çekme iþlemi baþarýsýz!Lütfen yeniden deneyiniz...");
sonucMesaj.setSonucTip(Constants.MESSAGE_FAILURE);
}
return sonucMesaj;
}
// [EXTRA] method!
public SonucMesaj extra(String hqlQuery) {
/**/
System.out.println("Extra...");
/**/
initializeSession();
/**/
System.out.println("Gelen query = " + hqlQuery);
/**/
List list = theSession.createQuery(hqlQuery).list();
/**/
System.out.println("Liste : " + list);
/**/
theSession.flush();
finalizeSession(theSession);
System.out.println(hqlQuery + " is okay");
if (jobFlag) {
sonucMesaj.setSonucMesajString("Ýþlem baþarý ile tamamlandý.Sorgulama iþlem sonucu :");
sonucMesaj.setSonucListe(list);
sonucMesaj.setSonucTip(Constants.MESSAGE_SUCCESS);
} else {
sonucMesaj.setSonucMesajString("Ýþlem yapýlýrken bir sorun oluþtu!Lütfen yeniden deneyiniz...");
sonucMesaj.setSonucTip(Constants.MESSAGE_FAILURE);
}
return sonucMesaj;
}
// Get specifed object!
/*private static void setCastedObject(Object object) {
if (object instanceof HesapPlan) {
valueObject = (HesapPlan) object;
theClassName = "HesapPlan";
}
}*/
// Does session's first common jobs!
private void initializeSession() {
theSession = HibernateUtil.getSessionFactory().getCurrentSession();
theSession.beginTransaction();
System.out.println("Starting new transaction...");
sonucMesaj = new SonucMesaj();
}
// Does session's last common jobs!
private void finalizeSession(Session session) {
try {
session.connection().commit();
System.out.println("Transaction is committed!");
jobFlag = true;
} catch (SQLException sqle) {
jobFlag = false;
System.out.println("Transaction can not be completed properly!");
System.out.println("Cause is : ");
sqle.printStackTrace();
try {
System.out.println("Transaction will be rollbacked...");
session.connection().rollback();
System.out.println("Rollback is okay!");
} catch (SQLException sqler) {
System.out.println("Rollback can not be done!");
System.out.println("Cause is : ");
sqler.printStackTrace();
}
}
session.close();
System.out.println("The -> " + session + " session is closed");
}
// Refresher...
private List getRefreshedList(Object obj) {
List returnValue = null;
SonucMesaj ss = select(obj);
if (ss != null && ss.getSonucTip() == Constants.MESSAGE_SUCCESS) {
returnValue = ss.getSonucListe();
}
return returnValue;
}
// Fields...
private static HibernateUtil theInstance = null;
private static SessionFactory sessionFactory;
private SonucMesaj sonucMesaj;
private boolean jobFlag;
private Object valueObject;
private Session theSession;
private String theClassName = "";
}
Here, the "SonucMesaj" returns the result. It is my special class. 'valueObject' field holds the core class which is mapped to a table in your mapper file. As you see, you will be able to hold only exactly one instance in memory for 'HibernateUtil' class. And you are free to give any name like "JohnUtil" it is not important. Two methods 'initializeSession' and 'finalizeSession' is called before each transaction in order.
Regards