This is my utility class used to obtain hibernate session.
package financing.tools.dgs.ws.hibDB;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUtility{
private static final SessionFactory sessionFactory;
static {
try {
// Create 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);
throw new ExceptionInInitializerError(ex);
}
}
public HibernateUtility(){
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public void insert(Object beanClass){
Session session = getSessionFactory().openSession();
session.save(beanClass);
}
public void update(Object bean){
Session session = getSessionFactory().openSession();
session.merge(bean);
session.flush();
}
public Object select(Serializable uniqueIdentifier,Class beanclass){
Object beanObject = null;
Session session = getSessionFactory().getCurrentSession();
beanObject =session.load(beanclass, uniqueIdentifier);
session.flush();
return beanObject;
}
public void delete(Object beanClass){
Session session = getSessionFactory().getCurrentSession();
session.delete(beanClass);
session.flush();
}
public Object select1(Serializable uniqueIdentifier,Class beanclass){
Object beanObject = null;
Session session = getSessionFactory().openSession();
beanObject =session.get(beanclass, uniqueIdentifier);
session.flush();
return beanObject;
}
}
Now i am calling this utility in my HandlerHibernate class code.The snippet of calling utility class is pasted here. I am using JTA transaction for this.
UserTransaction tx = (UserTransaction)new InitialContext()
.lookup("java:comp/UserTransaction");
try{
tx.begin();
weblog1 = (Weblog)hb.select1(uniqueKey, Weblog.class) ;
hb.delete(weblog1);
tx.commit();
}catch(RuntimeException e){
tx.rollback();
e.printStackTrace();
}
Code:
My mapping file for tables:
<class name="financing.tools.dgs.ws.hibDB.Weblog" table="GCPS.WEB_SERVICES_LOG1"
dynamic-update="true">
<composite-id
name="composite_key"
class="financing.tools.dgs.ws.hibDB.Composite" >
<key-property name="application_Name" column="APPLICATION_NAME" type="string" />
<key-property name="document_Number" column="DOCUMENT_NUMBER" type="string"/>
<key-property name="create_Timestamp" column="CREATE_TIMESTAMP" type="timestamp"/>
</composite-id>
<property name="service_Request_Response" type="string" column="SERVICE_REQUEST_RESPONSE"/>
<property name="fault_Occured" type="character" column="FAULT_OCCURRED"/>
<set name="websoap" cascade="delete" lazy="true" >
<key not-null="true">
<column name= "APPLICATION_NAME" />
<column name="DOCUMENT_NUMBER"/>
<column name="CREATE_TIMESTAMP"/>
</key>
<one-to-many class="financing.tools.dgs.ws.hibDB.Websoap1" />
</set>
<set name="webattachments" cascade="all-delete-orphan" lazy="true">
<key not-null="true">
<column name= "APPLICATION_NAME" />
<column name="DOCUMENT_NUMBER"/>
<column name="CREATE_TIMESTAMP"/>
</key>
<one-to-many class="financing.tools.dgs.ws.hibDB.Webattachments" />
</set>
<query name="byServiceName"><![CDATA[from financing.tools.dgs.ws.hibDB.Weblog as log where log.service_Request_Response = ?]]></query>
</class>