休眠配置 hbm2ddl.auto

Hibernate configuration hbm2ddl.auto

我有这样的 hibernate.cfg.xml:

<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:postgresql://host:5432/app-dev</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="hbm2ddl.auto">validate</property>
...many mappings
</session-factory>

问题是它正在尝试更新我的数据库架构,但我想禁用该功能。

来自我的应用程序的日志:

2015-08-29 16:29:57 ERROR SchemaUpdate:261 - HHH000388: Unsuccessful: create table myschema.public.mytable (id int4 not null, count int4, anotherid int4, onemoreid int4, primary key (id))
2015-08-29 16:29:58 ERROR SchemaUpdate:262 - ERROR: syntax error at or near "-"          <<(mydatabase name contains "-" sign)

排名:27

我还尝试将 hbm2ddl.auto 标记留空或在其中包含 'none' 值。

完全删除 属性 <property name="hbm2ddl.auto">validate</property>

默认情况下所有选项都为 false,hibernate 不会尝试进行任何更新。

删除 Hibernatehbm2ddl.auto 默认为 Hibernate 不执行任何操作。

来自docs

The hbm2ddl.auto option turns on automatic generation of database schemas directly into the database. This can also be turned off by removing the configuration option, or redirected to a file with the help of the SchemaExport Ant task.

Validate 是hbm2ddl.auto 的默认属性,即使你不指定hbm2ddl.auto 属性,它也是默认validate。其次没有"none"这样的值,只有4个选项:

  1. validate- 它只是检查 table 及其列是否存在于数据库中,如果 table 不存在或任何列不存在,则异常是抛出。

  2. create - 如果该值是创建的,那么 hibernate 会删除 table(如果它已经存在)然后创建一个新的 table 并执行操作,这在实时,因为旧数据从数据库中丢失。

  3. update - 如果更新了值,那么 hibernate 将使用现有的 table,如果 table 不存在,则会创建一个新的 table 并执行操作。这个在实时中用的最多也经常用,推荐

  4. create-drop - 如果该值为 create-drop,则 hibernate 会创建一个新的 table 并在执行该操作后删除 table,此值将在测试休眠代码。

谢谢 享受编码