如何让Hibernate创建数据库SQL脚本以进行初始化和更新

3
Hibernate能够创建数据库结构。以下是使用JPA2配置的示例,其中包含<property name="hibernate.hbm2ddl.auto" value="create"/>
(在这种情况下使用Hibernate Entity Manager)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.connection.charSet" value="UTF-8"/>
            <!-- Uncomment the following two properties for JBoss only -->
            <!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
            <!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
        </properties>
    </persistence-unit>
</persistence>

如何让Hibernate创建数据库SQL脚本以进行初始化和更新?(可以手动运行或通过批处理执行),以及使用哪些工具进行操作。
是的,日志中有SQL语句(请参考Getting SQL script from Hibernate update),但这不是一个干净的解决方案。
目标是为每个版本发布准备自动化数据库升级的解决方案。

我想知道https://dev59.com/NEfRa4cB1Zd3GeqP9n8a是否适用。 - Paul Verest
1个回答

0

对于自动化数据库升级,可以考虑使用另一种专门设计用于此工作的工具。我建议使用Liquibase


通常管理员会手动执行SQL命令。让他学习新东西只是为了用其他方式做事情是行不通的想法。正确的方法应该是自动化。 - Paul Verest

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接