views:

324

answers:

2

I've a main method using SchemaUpdate to display at the console what tables to alter/create and it works fine in my Hibernate project:

 public static void main(String[] args) throws IOException {
  //first we prepare the configuration
  Properties hibProps = new Properties();
  hibProps.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jbbconfigs.properties"));
  Configuration cfg = new AnnotationConfiguration();
  cfg.configure("/hibernate.cfg.xml").addProperties(hibProps);

  //We create the SchemaUpdate thanks to the configs
  SchemaUpdate schemaUpdate = new SchemaUpdate(cfg);


  //The update is executed in script mode only
  schemaUpdate.execute(true, false);
  ...  

I'd like to reuse this code in a JPA project, having no hibernate.cfg.xml file (and no .properties file), but a persistence.xml file (autodetected in the META-INF directory as specified by the JPA spec).

I tried this too simple adaptation,

Configuration cfg = new AnnotationConfiguration();
cfg.configure();

but it failed with that exception.

Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found

Has anybody done that? Thanks.

A: 

You should use Ejb3Configuration instead of the normal configuration. Refer to the entity manager documentation, at the end of the bootstrapping section in the hibernate documentation.

(copied from above source with minor adaptations)

Ejb3Configuration cfg = new Ejb3Configuration();
EntityManagerFactory emf = 
  cfg.addProperties( properties ) //add some properties
     .setInterceptor( myInterceptorImpl ) // set an interceptor
     .addAnnotatedClass( MyAnnotatedClass.class ) //add a class to be mapped
     .addClass( NonAnnotatedClass.class ) //add an hbm.xml file using the Hibernate convention
     .addResource( "mypath/MyOtherCLass.hbm.xml ) //add an hbm.xml file
     .addResource( "mypath/orm.xml ) //add an EJB3 deployment descriptor
     .configure("/mypath/hibernate.cfg.xml") //add a regular hibernate.cfg.xml
     .buildEntityManagerFactory(); //Create the entity manager factory

As you can see, you can mix a lot of different types of configuration.

If it's only for the schema update part, you can just set a property in your persistence.xml: hibernate.hbm2ddl.auto:

<persistence-unit name="app1">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <properties>
     …
     <property name="hibernate.hbm2ddl.auto" value="update"/>
   </properties>
</properties>

See here for a few more references.

Kariem
+1  A: 

Kariem is on the right track, but let me attempt to clarify.

Let's assume that you have a vanilla JPA-standard configuration with nothing Hibernate-specific, except for the Hibernate jars on the classpath. If you are running in J2SE bootstrap mode, you already have some code that looks something like this, either in Java or as Spring configuration, etc.:

Map<String, Object> props = getJPAProperties();
EntityManagerFactory emf = 
    Persistence.createEntityManagerFactory("persistence-unit-name", props);

To run SchemaUpdate, simply use this instead:

Map<String, Object> props = getJPAProperties();
Ejb3Configuration conf = 
    new Ejb3Configuration().configure("persistence-unit-name", props);
new SchemaUpdate(conf.getHibernateConfiguration()).execute(true, false);

I'm not sure how this would operate in a container environment, but in a simple J2SE or Spring type of configuration, that's all there is to it.

Peter Centgraf