-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Programmatic configuration using EJB3 annotated classes
PostPosted: Fri Jun 16, 2006 11:49 am 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 19, 2006 3:16 am 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
Nobody has any idea then? We're all in the same boat, including the Hibernate developers????


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 5:24 am 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
Right. The answer was, as usual found by hacking through the undergrowth, examining source code:

Code:
//         Configure our EJB3 EntityManagerFactory
            Ejb3Configuration ejbconf = new Ejb3Configuration();
         ejbconf.setProperty("hibernate.connection.datasource", "java:/" + datasourceName);
         ejbconf.setProperty("hibernate.dialect", dbType.getDialectClass());
         ejbconf.setProperty("jboss.entity.manager.factory.jndi.name", "java:/" + datasourceName + "Factory");
         ejbconf.setProperty("hibernate.show_sql", "false");
         ejbconf.setProperty("hibernate.ejb.naming_strategy", "com.fcl.greenfield.naming.FCLNamingStrategy");
         ejbconf.setProperty("hibernate.hbm2ddl.auto", "update");
         ejbconf.setProperty("hibernate.ejb.interceptor", "com.fcl.util.ReferenceInterceptor");
         ejbconf.setProperty("hibernate.ejb.event.post-commit-insert", "com.fcl.util.HibernateEventListener");
         ejbconf.setProperty("hibernate.ejb.event.post-commit-update", "com.fcl.util.HibernateEventListener");
         ejbconf.setProperty("hibernate.ejb.event.post-commit-delete", "com.fcl.util.HibernateEventListener");

//         Find all our persistent classes from persistence.jar
         JarVisitor.Filter[] filters = new JarVisitor.Filter[2];
         filters[0] = new JarVisitor.PackageFilter( false, null ) {
            public boolean accept(String javaElementName) {
               return true;
            }
         };
         filters[1] = new JarVisitor.ClassFilter(
               false, new Class[]{
               Entity.class,
               MappedSuperclass.class,
               Embeddable.class}
         ) {
            public boolean accept(String javaElementName) {
               return true;
            }
         };

//         Add all persistent classes to EJB3 config
         JarVisitor j = JarVisitor.getVisitor(Thread.currentThread().getContextClassLoader().getResource("persistence.jar"), filters);
           Set<JarVisitor.Entry>[] entries = (Set<JarVisitor.Entry>[])j.getMatchingEntries();
         List<String> classes = new ArrayList<String>();
         for (int i = 0; i < entries.length; i++)
         {
            for (JarVisitor.Entry c : entries[i])
            {
               Logger.info("Found entity " + c.getName());
               ejbconf.addAnnotatedClass(Class.forName(c.getName()));
            }
         }

           Logger.info("Building SessionFactory");
         EntityManagerFactory emf = ejbconf.createEntityManagerFactory();
         EntityManager em = emf.createEntityManager();



Perhaps this should be uuhhm, documented?????


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.