Hibernate has been edited by Karan Singh Malhi (Jul 31, 2008).

(View changes)

Content:

Sample persistence.xml

For a unit called "movie-unit" using two datasources called "movieDatabase" and "movieDatabaseUnmanaged" the following persistence.xml would work.

persistence.xml
<persistence version="1.0"
             xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

  <persistence-unit name="movie-unit">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>movieDatabase</jta-data-source>
    <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>

    <properties>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      <property name="hibernate.transaction.manager_lookup_class"
                value="org.apache.openejb.hibernate.TransactionManagerLookup"/>
    </properties>
  </persistence-unit>
</persistence>

Not using OpenEJB in production?

If you're using OpenEJB for testing and another platform, such as JBoss, in production a lookup strategy like the one below will allow you to get the best of both worlds and not have to change your persistence.xml. This "DynamicTransactionManagerLookup" class can be packed in your jar and deployed with your app.

DynamicTransactionManagerLookup.java
import org.hibernate.HibernateException;
import org.hibernate.transaction.TransactionManagerLookup;
import javax.transaction.TransactionManager;
import java.util.Properties;

public class DynamicTransactionManagerLookup implements TransactionManagerLookup {

    private TransactionManagerLookup impl;

    public DynamicTransactionManagerLookup() {
        String[] strategies = {
                "org.apache.openejb.hibernate.TransactionManagerLookup",
                "org.hibernate.transaction.JBossTransactionManagerLookup"
        };

        for (String className : strategies) {
            try {
                Class<?> clazz = this.getClass().getClassLoader().loadClass(className);
                impl = (TransactionManagerLookup) clazz.newInstance();
                break;
            } catch (Exception e) {
            }
        }

        if (impl == null) throw new IllegalStateException("No TransactionManagerLookup available");
    }

    public TransactionManager getTransactionManager(Properties properties) throws HibernateException {
        return impl.getTransactionManager(properties);
    }

    public String getUserTransactionName() {
        return impl.getUserTransactionName();
    }
}

Then set the Hibernate specific configuration property hibernate.transaction.manager_lookup_class to the name of the factory that you just created.

Some useful FYI from a user

I tried to use openejb 3.0 together with hibernate 3.2.6ga in a maven project. The transitive dependencies of openejb and hibernate collide on asm.jar. A similar problem is described here:
http://blog.springsource.com/main/2007/06/11/asm-version-incompatibilities-using-spring-autowired-with-hibernate/
or here:
http://forum.springframework.org/showthread.php?t=26713&highlight=cglib-nodep+hibernate
http://jira.springframework.org/browse/SPR-3856

The solution is to exclude the dependency on asm and cglib on hibernate and to add a dependency on cglib-nodep.

Reply via email to