I'm submitting here the cartridge that I've created for my application
that is not dependent on any EJB.
This cartridge generates code based on the DAO design pattern.
For each entity, the following classes are created:
- a DAO interface that contains CRUD methods
- an implementation of the DAO interface based on Hibernate.

This cartridge is based on the following assumptions:
- each com.foo.dao.xxxDAO methods throws com.foo.dao.DAOException (that
is not generated by androMDA)
- the Hibernate implementation use the ThreadLocal design pattern (see
ttp://www.hibernate.org/42.html)

For this cartridge, I have added a couple of things for customizing the
codegeneration:

- Tag value for setting the table name: @andromda.hibernate.table
- PK are assumed to be of type long, so the PK generator is the native.
So it supports auto-number, sequences or hi/log generators, depending on
the target database
- Add the method 'findAll()' int the DAO interface
- Add the following attributes tags:
        * @andromda.hibernate.length
        * @andromda.hibernate.unique
        * @andromda.hibernate.not-null

/*
 * Created on Oct 13, 2003
 */
package net.sf.bugzy.domain.dao;

/**
 * @author <a href="mailto:[EMAIL PROTECTED]">Herve Tchepannou</a>
 */
public class DAOException extends Exception
{
    /**
     *
     */
    public DAOException()
    {
        super();
    }
    /**
     *
     */
    public DAOException(String message)
    {
        super(message);
    }
    /**
     *
     */
    public DAOException(Throwable cause)
    {
        super(cause);
    }
    /**
     *
     */
    public DAOException(String message, Throwable cause)
    {
        super(message, cause);
    }
}
/*
 * Created on Oct 5, 2003
 */
package net.sf.bugzy.domain.dao.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;


/**
 * @author <a href="mailto:[EMAIL PROTECTED]">Herve Tchepannou</a>
 */
public class ThreadLocalSession
{
    //~ Static fields/initializers -----------------------------------------------------------------

    private static final Log         LOG     = LogFactory.getLog( ThreadLocalSession.class );
    private static final ThreadLocal CONTEXT = new ThreadLocal(  );

    //~ Instance fields ----------------------------------------------------------------------------

    private Session _session;

    //~ Methods ------------------------------------------------------------------------------------

    public static Session currentSession(  )
        throws HibernateException
    {
        /* Getting the session */
        ThreadLocalSession session = ( ThreadLocalSession ) CONTEXT.get(  );
        if ( session == null )
        {
            if ( LOG.isDebugEnabled(  ) )
            {
                LOG.debug( "Creating a new session" );
            }

            session          = new ThreadLocalSession(  );
            session._session = HibernateSingleton.getInstance(  )
                                                 .getSessionFactory(  )
                                                 .openSession(  );
            CONTEXT.set( session );
        }
        else
        {
            if ( LOG.isDebugEnabled(  ) )
            {
                LOG.debug( "Recycling the current session" );
            }
        }

        return session._session;
    }

    public static void closeSession(  )
        throws HibernateException
    {
        ThreadLocalSession session = ( ThreadLocalSession ) CONTEXT.get(  );
        if ( session == null )
        {
            return;
        }

        /* Closing the session */
        if ( ( session._session != null ) && session._session.isOpen(  ) )
        {
            if ( LOG.isDebugEnabled(  ) )
            {
                LOG.debug( "Closing the session" );
            }

            session._session.close(  );
        }

        CONTEXT.set( null );
    }
}

Attachment: andromda-dao.zip
Description: Zip archive

Reply via email to