Hibernate version: I'm using version 2
Hi,
I'm writing a J2EE application and I'm using sun's application server. My sessionbean to connect to the database is listed below (correct me if this code is totally wrong). Does anybody has a simple example that uses sessionbeans to connect to a database AND using the application server of sun.
Code:
/*
* Created on 30-dec-2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.sun.j2ee.blueprints.ruleengine;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.rmi.RemoteException;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.cfg.Environment;
/**
* @author Stijn Deroo-Van Maele
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DatabaseAccessBean implements SessionBean{
private SessionFactory sessions;
public DatabaseAccessBean() throws HibernateException{
configure();
}
////////////////////////////////////////////
/// METHODS ///
////////////////////////////////////////////
/**
* The configuration properties will be set here.
*
* @throws HibernateException
*/
public void configure() throws HibernateException {
sessions = new Configuration().addClass(Person.class)
.setProperty(Environment.HBM2DDL_AUTO, "create") // Autocreate the database schema.
.buildSessionFactory();
}
public void Store(Person p) throws HibernateException {
Session session = sessions.openSession();
Transaction tx = null;
try {
tx=session.beginTransaction();
session.save(p);
tx.commit();
} catch (HibernateException he) {
if (tx!=null) tx.rollback();
throw he;
}
finally {
session.close();
}
}
public void ejbCreate() throws HibernateException {configure();}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}
Regards,
Stijn.