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

前端 未结 3 566
再見小時候
再見小時候 2021-02-04 05:06

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(         


        
3条回答
  •  余生分开走
    2021-02-04 05:19

    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:

    
       org.hibernate.ejb.HibernatePersistence
       
       
    
    

    See here for a few more references.

提交回复
热议问题