JPA 和 toplink create-table 如果它们尚不存在?

发布于 2024-09-11 02:33:11 字数 607 浏览 10 评论 0原文

看来jpa是让我问很多问题的东西。

添加此功能后,

<property name="toplink.ddl-generation" value="create-tables"/>

我的 JPA 应用程序总是在运行时创建表,如果表已存在,这会导致异常。我希望 JPA 检查表是否已经存在,如果没有创建它们,但是我找不到上面执行此操作的属性的值。

因此,如果我只是将其关闭,是否有办法在某个时刻手动告诉 JPA 创建所有表?

异常

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'tags' already exists
Error Code: 1050
Call: CREATE TABLE tags (ID BIGINT AUTO_INCREMENT NOT NULL, NAME VARCHAR(255), OCCURRENCE INTEGER, PRIMARY KEY (ID))

更新这里是我得到MySQLSyntaxErrorException 的 ?!现在这肯定是错误的

Looks like jpa is something which makes me ask a lot of questions.

Having added this

<property name="toplink.ddl-generation" value="create-tables"/>

my JPA application always creates tables when running, which results in exceptions in case the tables already exist. I would like JPA to check if the tables already exist and if not create them, however I could not find a value for the property above which does this.

So if I just turn it off, is there a way to tell JPA manually at some point to create all the tables?

Update here's the exception I get

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'tags' already exists
Error Code: 1050
Call: CREATE TABLE tags (ID BIGINT AUTO_INCREMENT NOT NULL, NAME VARCHAR(255), OCCURRENCE INTEGER, PRIMARY KEY (ID))

MySQLSyntaxErrorException?! Now that's wrong for sure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你不是我要的菜∠ 2024-09-18 02:33:11

根据 http://www .oracle.com/technology/products/ias/toplink/JPA/essentials/toplink-jpa-extensions.html#Java2DBSchemaGen toplink没有更新现有表的选项,我不确定我是否会信任无论如何,它要做正确的事。
您可以配置 toplink 来生成一个 sql 脚本,然后您必须手动执行该脚本来创建所有表。文件名和位置可以这样配置:

<property name="toplink.ddl-generation" value="create-tables"/>
<property name="toplink.ddl-generation.output-mode" value="sql-script"/>
<property name="toplink.create-ddl-jdbc-file-name" value="createDDL.sql"/>
<property name="toplink.drop-ddl-jdbc-file-name" value="dropDDL.sql"/>
<property name="toplink.application-location" value="/tmp"/>

According to http://www.oracle.com/technology/products/ias/toplink/JPA/essentials/toplink-jpa-extensions.html#Java2DBSchemaGen toplink does not have an option to update exiting tables, I'm not sure if I would trust it to do the right thing anyway.
You could configure toplink to generate a sql script that you then would have to execute manually to create all tables. The filenames and location can be configured like this:

<property name="toplink.ddl-generation" value="create-tables"/>
<property name="toplink.ddl-generation.output-mode" value="sql-script"/>
<property name="toplink.create-ddl-jdbc-file-name" value="createDDL.sql"/>
<property name="toplink.drop-ddl-jdbc-file-name" value="dropDDL.sql"/>
<property name="toplink.application-location" value="/tmp"/>
最初的梦 2024-09-18 02:33:11

我希望[我的] JPA [provider] 检查这些表是否已存在,如果不存在则创建它们,但是我找不到上面执行此操作的属性的值。

奇怪的是,根据有关 toplink.ddl- Generation 扩展的 TopLink Essentials 文档,create-table 应保持现有表不变:

TopLink JPA 扩展用于架构生成

指定数据描述符语言
(DDL) 生成您想要的操作
您的 JPA 实体。指定 DDL
生成目标,参见
toplink.ddl- Generation.output-mode

有效值: oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider

如果您在外部使用持久性
EJB 容器并希望
创建 DDL 文件而不创建
表,另外定义一个Java
系统属性 INTERACT_WITH_DB
将其值设置为 false

I would like [my] JPA [provider] to check if the tables already exist and if not create them, however I could not find a value for the property above which does this.

Weird, according to the TopLink Essentials documentation about the toplink.ddl-generation extension, create-table should leave existing table unchanged:

TopLink JPA Extensions for Schema Generation

Specify what Data Descriptor Language
(DDL) generation action you want for
your JPA entities. To specify the DDL
generation target, see
toplink.ddl-generation.output-mode.

Valid values: oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider

If you are using persistence outside
the EJB container and would like to
create the DDL files without creating
tables, additionally define a Java
system property INTERACT_WITH_DB and
set its value to false.

木森分化 2024-09-18 02:33:11

Liquibase (http://www.liquibase.org) 擅长于此。完全适应它需要一些时间,但我认为这是值得的。

Liquibase 方式独立于您使用的 JPA 持久性提供程序。实际上,它甚至与数据库无关。

Liquibase (http://www.liquibase.org) is good at this. It takes some time to get fully used to it, but I think it's worth the effort.

The Liquibase-way is independent of which JPA persistence provider you use. Actually, it's even database agnostic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文