使用Hibernate 4生成SQL DB创建脚本
我们目前正在使用Hibernate 3,并使用Hibernate工具为DB模式生成SQL脚本 我们使用以下Ant任务使用Hibernate 4生成SQL DB创建脚本,hibernate,ant,Hibernate,Ant,我们目前正在使用Hibernate 3,并使用Hibernate工具为DB模式生成SQL脚本 我们使用以下Ant任务 <hibernatetool destdir="${target}"> <jpaconfiguration persistenceunit="@{persistenceUnit}" propertyfile="@{propertyfile}"/> <classpath refid="@{classpathid}"/> &
<hibernatetool destdir="${target}">
<jpaconfiguration persistenceunit="@{persistenceUnit}" propertyfile="@{propertyfile}"/>
<classpath refid="@{classpathid}"/>
<!-- the file name is relative to $destdir -->
<hbm2ddl outputfilename="@{output}" format="true" export="false" drop="false"/>
</hibernatetool>
我们想切换到Hibernate 4:没有Hibernate工具,我们如何实现类似的功能?您可以直接使用该类生成DDL脚本:
对于Hibernate 4:
Configuration config=new Configuration();
属性=新属性();
properties.put(“hibernate.dialogue”、“org.hibernate.dialogue.postgresqldialdialogue”);
properties.put(“hibernate.connection.url”,“jdbc:postgresql://localhost:5432/Test");
properties.put(“hibernate.connection.username”、“username”);
properties.put(“hibernate.connection.password”、“password”);
properties.put(“hibernate.connection.driver_class”,“org.postgresql.driver”);
properties.put(“hibernate.show_sql”,“true”);
config.setProperties(属性);
config.addAnnotatedClass(MyMappedPojo1.class);
config.addAnnotatedClass(MyMappedPojo2.class);
..................
SchemaExport SchemaExport=新SchemaExport(配置);
schemaExport.setDelimiter(“;”);
/**只需将模式SQL转储到控制台,而不执行它们***/
schemaExport.create(true,false);
Hibernate 5的更新:
Properties属性=新属性();
properties.put(“hibernate.dialogue”、“org.hibernate.dialogue.postgresqldialdialogue”);
properties.put(“hibernate.connection.url”,“jdbc:postgresql://localhost:5432/Test");
properties.put(“hibernate.connection.username”、“username”);
properties.put(“hibernate.connection.password”、“password”);
properties.put(“hibernate.connection.driver_class”,“org.postgresql.driver”);
properties.put(“hibernate.show_sql”,“true”);
StandardServiceRegistry serviceRegistry=新的StandardServiceRegistryBuilder()
.applySettings(properties.build();
MetadataSources metadataSource=新的MetadataSources(serviceRegistry);
metadataSource.addAnnotatedClass(MyMappedPojo1.class);
metadataSource.addAnnotatedClass(MyMappedPojo2.class);
...........
Metadata meta=metadataSource.buildMetadata();
SchemaExport SchemaExport=新SchemaExport();
schemaExport.setDelimiter(“;”);
schemaExport.execute(EnumSet.of(TargetType.STDOUT)、Action.CREATE、meta);