6

看起来 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?!现在肯定是错的

4

3 回答 3

5

根据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"/>
于 2010-07-19T10:58:43.567 回答
2

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

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

用于模式生成的 TopLink JPA 扩展

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.

于 2010-07-19T13:40:57.097 回答
0

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.

于 2010-07-19T16:11:07.447 回答