java Eclipselink 更新现有表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2945714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:36:40  来源:igfitidea点击:

Eclipselink update existing tables

javaormjpaeclipselink

提问by javydreamercsw

Maybe I got it wrong but i though that JPA was able to update an existing table (model changed adding a column) but is not working in my case.

也许我弄错了,但我认为 JPA 能够更新现有表(模型更改了添加列),但在我的情况下不起作用。

I can see in the logs eclipselink attempting to create it but failing because it already exists. Instead of trying an update to add the column it keeps going.

我可以在日志 eclipselink 中看到尝试创建它但失败了,因为它已经存在。它没有尝试更新以添加列,而是继续进行。

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jwrestling"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
<property name="eclipselink.logging.level" value="INFO"/>

And here's the table with the change (online column added)

这是更改后的表格(添加了在线列)

[EL Warning]: 2010-05-31 14:39:06.044--ServerSession(16053322)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.1.0.v20100517-r7246): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'account' already exists
Error Code: 1050
Call: CREATE TABLE account (ID INTEGER NOT NULL, USERNAME VARCHAR(32) NOT NULL, SECURITY_KEY VARCHAR(255) NOT NULL, EMAIL VARCHAR(64) NOT NULL, STATUS VARCHAR(8) NOT NULL, TIMEDATE DATETIME NOT NULL, PASSWORD VARCHAR(255) NOT NULL, ONLINE TINYINT(1) default 0 NOT NULL, PRIMARY KEY (ID))
Query: DataModifyQuery(sql="CREATE TABLE account (ID INTEGER NOT NULL, USERNAME VARCHAR(32) NOT NULL, SECURITY_KEY VARCHAR(255) NOT NULL, EMAIL VARCHAR(64) NOT NULL, STATUS VARCHAR(8) NOT NULL, TIMEDATE DATETIME NOT NULL, PASSWORD VARCHAR(255) NOT NULL, ONLINE TINYINT(1) default 0 NOT NULL, PRIMARY KEY (ID))")
[EL Warning]: 2010-05-31 14:39:06.074--ServerSession(16053322)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.1.0.v20100517-r7246): org.eclipse.persistence.exceptions.DatabaseException

After this it continues with the following.

在此之后,它继续进行以下操作。

Am I doing something wrong or is a bug?

我做错了什么还是一个错误?

回答by Jean-Philippe Pellet

As of EclipseLink 2.4, you can use this in the specification of your persistence unit:

从 EclipseLink 2.4 开始,您可以在持久化单元的规范中使用它:

<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />

回答by Pascal Thivent

This is the expected behavior when using the create-tablesvalue. From the documentation about the eclipselink.ddl-generationproperty:

这是使用该create-tables值时的预期行为。从关于eclipselink.ddl-generation财产的文件:

Using EclipseLink JPA Extensions for Schema Generation

The following are the valid values for the use in a persistence.xmlfile:

  • none– EclipseLink does not generate DDL; no schema is generated.
  • create-tables– EclipseLink will attempt to execute a CREATE TABLESQL for each table. If the table already exists, EclipseLink will follow the default behavior of your specific database and JDBC driver combination (when a CREATE TABLESQL is issued for an already existing table). In most cases an exception is thrown and the table is not created. EclipseLink will then continue with the next statement. (See also eclipselink.create-ddl-jdbc-file-name.)
  • drop-and-create-tables– EclipseLink will attempt to DROPall tables, then CREATEall tables. If any issues are encountered, EclipseLink will follow the default behavior of your specific database and JDBC driver combination, then continue with the next statement. (See also eclipselink.create-ddl-jdbc-file-nameand eclipselink.drop-ddl-jdbc-file-name.)

使用 EclipseLink JPA 扩展进行模式生成

以下是在persistence.xml文件中使用的有效值:

  • none– EclipseLink 不生成 DDL;没有生成模式。
  • create-tables– EclipseLink 将尝试CREATE TABLE为每个表执行SQL。如果该表已存在,EclipseLink 将遵循您的特定数据库和 JDBC 驱动程序组合的默认行为(当CREATE TABLE为已存在的表发出 SQL时)。在大多数情况下,会抛出异常并且不会创建表。EclipseLink 将继续执行下一条语句。(另见 eclipselink.create-ddl-jdbc-file-name。)
  • drop-and-create-tables– EclipseLink 将尝试DROP所有表,然后是CREATE所有表。如果遇到任何问题,EclipseLink 将遵循您的特定数据库和 JDBC 驱动程序组合的默认行为,然后继续执行下一条语句。(另见 eclipselink.create-ddl-jdbc-file-nameeclipselink.drop-ddl-jdbc-file-name。)

So you might want drop-and-create-tablesinstead.

所以你可能想要drop-and-create-tables

回答by joshua

Hibernate JPA implementation does exactly what you want. Here is the property that enables that behaviour:

Hibernate JPA 实现正是您想要的。这是启用该行为的属性:

property name="hibernate.hbm2ddl.auto" value="update"

回答by Hamdi Baligh

create-or-extend-tables does the trick

创建或扩展表可以解决问题