1

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 语句(Q Getting SQL script from Hibernate update),但这不是干净的解决方案。

目标是为每个版本的自动数据库升级准备解决方案。

4

1 回答 1

0

对于自动数据库升级,请查看专门为该工作设计的另一个工具。我会建议Liqiubase

于 2013-08-14T07:18:52.463 回答