Auto generate data schema from JPA annotated entity classes

后端 未结 9 1555
滥情空心
滥情空心 2020-12-12 17:43

I\'m using JPA (Hibernate\'s implementation) to annotate entity classes to persist to a relational database (MySQL or SQL Server). Is there an easy way to auto generate the

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-12 18:37

    As Hibernate 4.3+ now implements JPA 2.1 the appropriate way to generate DDL scripts is to use following set of JPA 2.1 properties :

    
    
    
    

    As it will be run at runtime, you may want to execute this DDL generation at build. There is no supported official maven plugin anymore for Hibernate4 probably because Hibernate team is moving to Gradle.

    Anyway, this is the JPA 2.1 approach to generate this script programmatically :

    import java.io.IOException;
    import java.util.Properties;
    
    import javax.persistence.Persistence;
    
    import org.hibernate.jpa.AvailableSettings;
    
    public class JpaSchemaExport {
    
        public static void main(String[] args) throws IOException {
            execute(args[0], args[1]);
            System.exit(0);
        }
    
        public static void execute(String persistenceUnitName, String destination) {
            System.out.println("Generating DDL create script to : " + destination);
    
            final Properties persistenceProperties = new Properties();
    
            // XXX force persistence properties : remove database target
            persistenceProperties.setProperty(org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO, "");
            persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "none");
    
            // XXX force persistence properties : define create script target from metadata to destination
            // persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SCHEMAS, "true");
            persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_ACTION, "create");
            persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SOURCE, "metadata");
            persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_CREATE_TARGET, destination);
    
            Persistence.generateSchema(persistenceUnitName, persistenceProperties);
        }
    
    }
    

    As you can see it's very simple !

    You can now use this in an AntTask, or MAVEN build like this (for MAVEN) :

                
                    maven-antrun-plugin
                    1.7
                    
                        
                            generate-ddl-create
                            process-classes
                            
                                run
                            
                            
                                
                                    
                                    
                                        
                                        
                                        
                                        
                                    
                                
                            
    
                        
                    
                
    

提交回复
热议问题