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

时间:2013-08-14 07:05:13

标签: java sql hibernate initialization

Hibernate能够创建数据库结构。以下是使用<property name="hibernate.hbm2ddl.auto" value="create"/><?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 Entity Manager)

{{1}}

如何让Hibernate创建用于初始化和更新的数据库SQL脚本?(可以手动运行或通过批量执行)以及用于此的工具。

是的,日志中有SQL语句(Q JPA2),但这不是干净的解决方案。

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

1 个答案:

答案 0 :(得分:0)

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