Simple guide on using ejb’s with hibernate

Last updated: 20-Nov-2003

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 wrote in the
purpose that it might spare someone the 2 days it took me to start everything.

Thaks to
all writting documentation for these wonderfull projects.

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

  2. Generate
    the classes and do a 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 http://www.len.ro/work/articles/jboss/ejb-remoting/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=”http://www.len.ro/work/articles/jboss/ejb-remoting/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 to safe 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 done easy 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.


Links: