|
Page Edited :
OPENEJBx30 :
Hibernate
Hibernate has been edited by Karan Singh Malhi (Jul 31, 2008). Content:Sample persistence.xmlFor 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.
|
Unsubscribe or edit your notifications preferences
