HIBERNATE JBoss.org
 |  Register  | 
     
News 
About 
   Feature List 
   Road Map 
Documentation 
   Related Projects 
   External Documentation 
Download 
Forum & Mailinglists 
Support & Training 
JIRA Issue Tracking
Wiki Community Area


Hibernate Public Training Courses


Get Hibernate in Action eBook!


JavaWorld 2003 Finalist


Jolt Award 2004 Winner
      
Documentation > Community Area > Simple startup for EJBs with Hibernate

Simple startup for EJBs with Hibernate

Disclaimer: this document is mainly a compilation from different sources (see links) and the result of the effort to put everything together.

Purpose: this is merely a step-by-step document ilustrating how to put everything together assuming that you know all the components. It is written for the purpose that it might spare someone the 2 days it took me to start everything.

This document main location

Thanks to all writting documentation for these wonderful projects.

1. Write the hibernate mapping file and the hibernate.properties (usully I use this approach for maximum flexibility).

2. Generate the classes and run SchemaExport

3. Configure postgresql support in jboss. Copy postgresql.jar (or what ever name your jar might have) to the ${jboss.home}/server/default/lib directory. Edit (set your db config) and copy the postgres-service.xml to ${jboss.home}/server/default/deploy directory.

4. Configure hibernate support in jboss. Copy the following jars, which can be found in ${hibernate.home}/lib, to the ${jboss.home}/server/default/lib directory:

  • cglib.jar
  • commons-collections.jar
  • commons-logging.jar
  • commons-lang.jar
  • jcs.jar
  • odmg.jar
  • hibernate.jar

5. I used an ant task to create and deploy the sar.

<target name="sar" depends="compile">
      <mkdir dir="${build.deploy.dir}"/>
      <jar jarfile="${build.deploy.dir}/hibernate.sar" index="true">
        <metainf dir="${basedir}/conf">
          <include name="jboss-service.xml"/>
        </metainf>
        <fileset dir="${basedir}" includes="*.hbm.xml"/>
        <fileset dir="${build.classes.dir}" includes="ro/nit/pilot/data/**" >
        </fileset>
      </jar>

      <copy todir="${jboss.deploy}">
        <fileset dir="${build.deploy.dir}" includes="*.sar">
        </fileset>
      </copy>

6. At this point you can test the config by starting jboss. If you see no error then everything is ok!

7. Writing the ejb's. I used an approach similar to the DAO pattern. The classes are: bussinessObject (session bean - XBean), DAO (XDao), DataSource (this is hibernate), value object (the class generated from the mapping xml - XData).

8. Access hibernate from the ejbs. Get the Session object:

Context ctx = new InitialContext();
SessionFactory factory = (SessionFactory) ctx.lookup("java:/hibernate/HibernateFactory");
sess = factory.openSession();

Observation: if you use a statefull session bean then you might think it is safe to save the sess object. DO NOT do that since the container might want to passivate the bean and the sess can not be serialized while it holds an open connection.

9. Write a method in the session bean - XBean:

      /**
      * @ejb:bean name="pilot/X"
      * display-name="X Bean"
      * type="Stateful"
      * transaction-type="Container"
      * jndi-name="pilot/X"
      * view-type="remote"
      *
      * @ejb:transaction type="Supports"
      *
      * @ejb:permission unchecked=""
      *
      **/
      public class XBean implements SessionBean{
      
      /**
      * @ejb:interface-method view-type="remote"
      * @ejb:permission role-name="user"
      **/
      public XData getX(long xId) throws RemoteException, BusinessException{

      ..... fetch the sess .........

      try{
           X DAO dao = new XDAO(sess);
           return dao.getX(xId);
      }catch(BusinessException e){
           mContext.setRollbackOnly();
           throw e;
      }finally{
           sess.close();
      }

      ..... usual ejb code ........

      }

10. Write the corresponding DAO method. That is easily done using hibernate via the sess object.

11. Run XDoclet to generate everything. You can find a lot of information on this. I used the example s from jboss (works with xdoclet 1.1.2) and the one from middlegen (works with xdoclet 1.2.b2)

12. Call the method remotely or where ever you want.

13. Implement everything else :). I use XgMLui for the gui part.

Links:

Hibernate

Using Hibernate with JBoss

JMX in JBoss

JBoss

This document

XDoclet

      

coWiki