Saturday, July 24, 2010

Unit Testing your JPA entities

If you want to avoid exceptions like this at runtime after adding properties to your JPA entities, it might be worthwhile to invest some time in writing unit tests for them.
Caused by: org.hibernate.MappingException: Could not determine type for: be.ecs.validation.entities.City, at table: Person, for columns: [org.hibernate.mapping.Column(city)]
at org.hibernate.mapping.SimpleValue.getType(
at org.hibernate.tuple.PropertyFactory.buildStandardProperty(
at org.hibernate.tuple.component.ComponentMetamodel.(
at org.hibernate.mapping.Component.buildType(
at org.hibernate.mapping.Component.getType(
at org.hibernate.mapping.SimpleValue.isValid(
at org.hibernate.mapping.Property.isValid(
at org.hibernate.mapping.PersistentClass.validate(
at org.hibernate.mapping.RootClass.validate(
at org.hibernate.cfg.Configuration.validate(
at org.hibernate.cfg.Configuration.buildSessionFactory(
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(
... 43 more

The Spring TestContext Framework provides generic, annotation-driven unit and integration testing support. More information can be found on

The following needs to be done
  1. Create a persistence.xml specific to your unit tests
  2. Create a Spring application context to bootstrap the entitymanager
  3. Create the unit test that
  • constructs an entity
  • persists it
  • retrieves it
    1. Create a persistence.xml specific to your unit tests
    We'll create a dedicated persistence.xml file in our package (src/test/java/META-INF/persistence.xml). In this example, we're using an in-memory HSQL database.

    <persistence-unit name="pu" transaction-type="RESOURCE_LOCAL">
    <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-testing-jpa"/>
    <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
    <property name="" value="create-drop"/>
    <property name="hibernate.connection.username" value="sa"/>
    <property name="hibernate.connection.password" value="x"/>
    <property name="hibernate.archive.autodetection" value="class, hbm"/>

    2. Create a Spring application context



    <bean id="entityManagerFactory"
    <property name="persistenceUnitName" value="pu" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />

    3. Create the unit test

    public class PersonPersistenceTest {

    private EntityManager entityManager;

    public EntityManager getEntityManager() {
    return entityManager;

    public void createAndPersistPerson() {
    Person person = new Person();
    Person retrievedPerson = getEntityManager().find(Person.class, person.getId());


    Seam returns HTTP Status 404 but the page exist

    Did it ever occur to you that you saw this message while are sure that particular page does exist at the given location ? :

    Always keep an eye on the Seam console output, as in this case it provides the following info :

    Jul 24, 2010 3:14:46 PM com.sun.facelets.impl.DefaultFaceletFactory createFacelet
    WARNING: /conversations/template/template.xhtml not found at jndi:/localhost/seamdemo.ui/conversations/template/template.xhtml
    The root cause for the 404 is the fact that his particular page referred to a template that couldn't be located :

    <ui:composition xmlns=""
    <ui:define name="body">

    As you can see, the template (defined in a relative way) couldn't be located from the page consuming it (located in a different folder).