-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: HibernateUtil class..
PostPosted: Fri Feb 10, 2006 3:21 am 
Newbie

Joined: Thu Feb 09, 2006 5:04 am
Posts: 1
Actually i am very new user for Hibernate. And i have written one program on hibernate . I am using Oracle JDeveloper 10g IDE. I included all the libraries. But its giving error on only one thsng i.e . its not going to get HibernateUtil class. so pls let me confirm from which package we can get this(HibernateUtil) class.

I imported these packages...

import org.hibernate.*;
import org.hibernate.cfg.*;

so what else i have to import..

i have wriien a very simple program for testing..

pls anyone can help me..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 3:58 am 
Newbie

Joined: Thu Feb 09, 2006 1:22 pm
Posts: 13
Location: Turkey
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

_________________
eRoHaN


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.