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

前端 未结 3 561
再見小時候
再見小時候 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:34

    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 props = getJPAProperties();
    EntityManagerFactory emf = 
        Persistence.createEntityManagerFactory("persistence-unit-name", props);
    

    To run SchemaUpdate, simply use this instead:

    Map 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.

提交回复
热议问题