How to use Hibernate SchemaUpdate class with a JPA persistence.xml?

Posted by John Rizzo on Stack Overflow See other posts from Stack Overflow or by John Rizzo
Published on 2010-04-15T12:41:57Z Indexed on 2010/04/17 22:43 UTC
Read the original article Hit count: 252

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about jpa