I'm using EJB3 annotated classes within JBoss.
I've asked here before about programmatic configuration, but nobody seemed to know much, and I went with having a "persistence.xml" in my classpath, and Hibernate picked that up.
Now I really need to know how to configure the Hibernate Entity Manager programatically.
And though I consider myself an experienced developer I am
hopelessly lost!
There are about 10 different classes all involved in "config" in some way. Some use XML, but is is the persistence.xml type of XML, or the hibernate.cfg.xml type of xml?
How do I go about it? It should be simple. I just want to point Hibernate at a jar which contains annotated classes, give it info about the database (which is what persistence.xml does), and have it start up.
I must do it this way because on first run, this app asks what database to use, and then sets up configuration based on that, so it needs to store the configuration in its own format, and start Hibernate programatically on deployment.
I've been scouring the sources because the Javadocs are "blank". I've been through PersistenceXMLLoader - that seems useless, it just loads some info into a PersistenceUnitMetaData which you then CANNOT DO ANYTHING WITH.
So I've been looking at org.hibernate.cfg.Confiuration, and org.hibernate.cfg.AnnotationConfiguration but the Javadocs say nothing whatsoever about how they work. There's nothing on Google except javadocs in various places.
My persistence.xml (I've created a string containing it in the hope I can feed it, or a parsed version of it into one of the many, many configuration classes) look like this:
Code:
<?xml version="1.0"?>
<persistence-unit name="Nigel">
<description>The nigel tables for Company Foo</description>
<jta-data-source>java:/NigelDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/NigelDSFactory"/>
<!-- <property name="hibernate.cache.provider_class" value="org.jboss.ejb3.entity.TreeCacheProviderHook" /> -->
<!-- <property name="hibernate.treecache.mbean.object_name" value="jboss.cache:service=EJB3EntityTreeCache" /> -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.ejb.naming_strategy" value="com.fcl.greenfield.naming.FCLNamingStrategy" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.ejb.interceptor" value="com.fcl.util.ReferenceInterceptor"/>
<property name="hibernate.ejb.event.post-commit-insert" value="com.fcl.util.HibernateEventListener"/>
<property name="hibernate.ejb.event.post-commit-update" value="com.fcl.util.HibernateEventListener"/>
<property name="hibernate.ejb.event.post-commit-delete" value="com.fcl.util.HibernateEventListener"/>
</properties>
</persistence-unit>
I've also tried it using an AnnotationConfiguration, and adding the following XML
Code:
<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource" value="java:/NigelDS"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/NigelDSFactory"/>
<!-- <property name="hibernate.cache.provider_class" value="org.jboss.ejb3.entity.TreeCacheProviderHook" /> -->
<!-- <property name="hibernate.treecache.mbean.object_name" value="jboss.cache:service=EJB3EntityTreeCache" /> -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.ejb.naming_strategy" value="com.fcl.greenfield.naming.FCLNamingStrategy" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.ejb.interceptor" value="com.fcl.util.ReferenceInterceptor"/>
<property name="hibernate.ejb.event.post-commit-insert" value="com.fcl.util.HibernateEventListener"/>
<property name="hibernate.ejb.event.post-commit-update" value="com.fcl.util.HibernateEventListener"/>
<property name="hibernate.ejb.event.post-commit-delete" value="com.fcl.util.HibernateEventListener"/>
</session-factory>
</hibernate-configuration>
Note the setting of the hibernate.connection.datasource property and the hibernate.dialect property? Well from this I get
Code:
15:39:26,968 WARN [UserSuppliedConnectionProvider] No connection properties specified - the user must supply JDBC connections
15:47:58,640 ERROR [fcl] Hibernate Dialect must be explicitly set
I'm going round in circles! How do you configure your persistence units?