-->
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.  [ 1 post ] 
Author Message
 Post subject: EJB - session bean
PostPosted: Fri Dec 31, 2004 1:01 pm 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
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.


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

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.