Title: Hibernate
# Sample persistence.xml
For a unit called "movie-unit" using two datasources called "movieDatabase"
and "movieDatabaseUnmanaged" the following persistence.xml would work.
org.hibernate.ejb.HibernatePersistence
movieDatabase
movieDatabaseUnmanaged
Note that as of OpenEJB 3.1 you do not need to set the
_hibernate.transaction.manager_lookup_class_ as it will be set for you
automatically and will overwrite any "org.hibernate.transaction." strategy
class already set while leaving any custom strategy class you may have
implemented untouched. The result is that you can leave your
_hibernate.transaction.manager_lookup_class_ property configured to your
production environment and OpenEJB will update it just for the scope
testing. On the other hand if you have implemented a custom
_org.hibernate.transaction.TransactionManagerLookup_ strategy it will
always be used and never replaced by OpenEJB.
Note that if you need more functionality in this area we are always happy
to add it.
# Not using OpenEJB in production?
If you're using OpenEJB 3.0 which does not support the dynamic switching of
the _hibernate.transaction.manager_lookup_class_ this is one way to achieve
it.
A custom implementation of Hibernate's *TransactionManagerLookup* strategy
like the following will do the trick. This
"DynamicTransactionManagerLookup" class can be packed in your jar and
deployed with your app.
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.
{info:title=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.
{info}