生成与Hibernate 4的SQL数据库创建脚本(Generate an SQL DB creat

2019-07-17 12:28发布

目前,我们正在使用Hibernate 3,我们使用Hibernate工具生成数据库架构的SQL脚本。

我们用下面的Ant任务

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

我们想切换到休眠4:我们如何能够实现无休眠的工具类似的东西?

Answer 1:

您可以直接使用的SchemaExport类生成DDL脚本:

Configuration config = new Configuration();

Properties properties = new Properties();

properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
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(properties);

config.addAnnotatedClass(MyMappedPojo1.class);
config.addAnnotatedClass(MyMappedPojo2.class);
..................

SchemaExport schemaExport = new SchemaExport(config);
schemaExport.setDelimiter(";");

/**Just dump the schema SQLs to the console , but not execute them ***/
schemaExport.create(true, false);


文章来源: Generate an SQL DB creation script with Hibernate 4
标签: hibernate ant