Showing posts with label j5ee. Show all posts
Showing posts with label j5ee. Show all posts

2007-08-02

JUnit and JPA

So, we have a need to test EntityManager driven classes from inside our JUnit tests. How do you set this up? Well, I for one added a testDb persistence unit to my persistence.xml file and then set up this abstract class...


package com.vifprogram.tie.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.After;
import org.junit.Before;

/**
* This testCase object works in conjunction with a test persistence unit you
* define in your persistence.xml. It sets up an entity manager that is aware
* of all the classes in your build path.
*
* Inherit from this class to build unit tests which need an entity manager.
*
* @author shawn
*
*/
public abstract class EMTestCase {

private static final String TEST_PERSISTENCE_UNIT = "testDb";
private EntityManagerFactory emf;
protected EntityManager em;

public EMTestCase() {
super();
}

@Before
public void initEmfAndEm() {
emf = Persistence.createEntityManagerFactory(TEST_PERSISTENCE_UNIT);
em = emf.createEntityManager();
}

@After
public void closeEmfAndEm() {
em.close();
emf.close();
}

}

2007-05-30

Hibernate3 is not EJB3

My first and most painful lesson in my year with J5EE was about Hibernate, Hibernate3, annotations, and EJB3. I had been lead to believe by an evil leprechaun that EJB3 was just Hibernate. I learned by actual experience in creating applications that this is simply not true. In fact EJB3 is a standard that Hibernate3 implements.

Why is that an important distinction?

Because EJB3 features are a proper subset of Hibernate3 features since Hibernate "embraces and extends" the EJB3 standard. It also means that if you do intend on using those cute little "at" signs in a "portable" way you'll have to figure out what EJB3 annotation to use to solve the Hibernate trace you just saw.

EJB3 doesn't require XML sit-ups and the EJB3 implementation in JBoss that sits on top of the Hibernate3 layer hides those gut busting crunches from your beady little eyes. So using the EJB3 persistence feature is far less painful than using the Hibernate 2 XML workout routine. That would typically mean that you'd want to use the EJB3 model whenever possible.

The problem is that Annotations for persistence and XML for persistence can get into fistycuffs. If you set up a Hibernate SessionFactory (even if it's a version 3 session factory) you may use XML docs _or_ annotations... theory may say otherwise but when I sat down to mix my annotated classes with XML configured classes the programmatic changes to the configuration wouldn't stick.

Hibernate3 is not EJB3.

2007-05-29

A year with Java 5 EE

I've spent the last year with Java 5 Enterprise Edition betas and I'm going to talk about my experiences with the Java environment as a relative newcomer to the Java landscape. My background is in C/C++ System V programming on Linux, Solaris, and Irix systems. It has been a hard year but one that I'm thankful for. I've learned a lot about how Service Oriented Architectures go together, why they work, and why they fail.