Looking for a Maven2 EJB3 ready to run project? I came across this post. I gathered all the good ideas and grouped them together in ejb3-maven Google Code project.

https://code.google.com/p/ejb3-maven/

The project consists of three sub-projects - ejb, war and ear.

The changes I’ve made:

  • Replaced hsqldb with MySQL, featuring out-of-container JPA unit testing.
  • Replaced TestNG with JUnit4
  • Encapsulated the repeatable CRUD methods in abstract BaseDAO supertype (code bellow)
  • Fixed the ejb-api not-found Maven dependency discovery by adding additional repository (ejb-api is not available at default http://download.java.net/maven/2/ so I supplement with additional jboss repository http://repository.jboss.com/maven2/).
  • Provided sample JNDI utilities helping with session bean discovery

All said, I am looking forward to a complete rails solution where by simply providing the table names, the framework will generate all the ADO classes (in our case JPA DAO classes and entities). Probably this will be my next challenge …

And the sample BaseDao implementation is:

public interface BaseDao<E extends Indexable>{

	/**
	 * Dependency injection setter method.
	 * The method should be used at unit testing
         * during out-of-container DI injection.
	 */
	void setEntityManager(EntityManager em);

	/**
	 * Persists an instance of this new entity to the database
	 */
	E create(E entity);

	/**
	 * Returns a database entity using the given primary key ID.
	 */
	E read(Serializable id);

	/**
	 * Saves changes made to an entity.
	 */
	E update(E entity);

        /**
         * Deletes an entity
         */
	void delete(E entity);

        /**
         * Returns all entities of E type. Probably this
         * would work best with JPA Criteria..
         */
	List list();

        /**
         * Returns the entity 'E' type to current
         * DAO implementation
         */
	Class getType();
}
interface Indexable {
	/**
	 * @return the primary key id
	 */
	Long getId();

	/**
	 * @param id  the primary key id to set
	 */
	void setId(Long id);
}
class BaseDaoImp<E extends Indexable> implements BaseDao<E>{

	/**
	 * The JPA entity manager to be used from all derived DAO classes
	 */
	@PersistenceContext(unitName = Globals.JTA_PERSISTENT_UNIT)
	protected EntityManager em;

	/**
	 * The SessionContext to be used from all derived DAO classes
	 */
	@Resource
	protected SessionContext sc;

	/**
	 * This method should be used only in out-of-container environment
	 * injecting a NON-"JTA" ("RESOURCE_LOCAL") EntityManager
	 */
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	public final E create(E entity) {
		em.persist(entity);
		em.flush();
		return entity;
	}

	public final void delete(E entity) {
		em.remove(entity);
		em.flush();
	}

	public final E read(Serializable id) {
		return em.find(getType(), id);
	}

	public boolean exists(Serializable id) {
		return (null != read(id));
	}

	public E update(E entity) {
		em.merge(entity);
		em.flush();
		return entity;
	}

	public List list() {
		Query qry = em.createQuery("from " +
                     getType().getSimpleName());
		return qry.getResultList();
	}
}