通过 JPA 重写 Derby DB 表(使用 FK)

发布于 2025-01-02 13:51:58 字数 1823 浏览 3 评论 0原文

我可以在数据库中填充 2 个表,其中表具有映射(一个表与另一个表具有 FK)。这 EclipseLink DataSource 显示刷新到数据库已正常完成。 我正在尝试重写表(第二次使用相同的数据),而 persistence.xml 有 属性条款如下: 但我发现在重写之前没有删除(擦除)以前的表,因此被拒绝,并出现以下有关重复键的异常消息:

“内部异常: java.sql.SQLIntegrityConstraintViolationException:该语句是 中止,因为它会导致唯一的键值重复 或主键约束或唯一索引标识 在“HOST”上定义“SQL120206135740510”。 " ( HOST 是指向的表 由FK)。

我在 persistence.xml 中缺少什么,或者应该使表在重写之前删除? 下面看到 persistence.xml 文件(数据库文件称为“test”。架构是用户名后面的名称(在我的例子中是 APP)):

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
        <persistence-unit name="jpa_test"> 
                <class>Host</class> 
                <class>Vm</class> 
                <properties> 
                        <property name="javax.persistence.jdbc.url" value="jdbc:derby://im6-64s:1527/test;create=true" /> 
                        <property name="javax.persistence.jdbc.user" value="APP" /> 
                        <property name="javax.persistence.jdbc.password" value="passw0rd" /> 
                        <!-- EclipseLink should create the database schema automatically --> 
                        <property name="eclipselink.ddl-generation" value="drop-create-tables" /> 
                        <property name="eclipselink.ddl-generation.output-mode" value="database" /> 
                </properties> 

        </persistence-unit> 
</persistence> 

I can fill 2 tables in a DB where the tables have mappings(one table has a FK from he other). The
EclipseLink DataSource show the flush to the DB has been done ok.
I am trying to rewrite the tables (with same data a second time ) while the persistence.xml has
the properties clause as follows :
<property name="eclipselink.ddl-generation" value="drop-create-tables" />
yet I see no dropping (erase) of the previous tables before the rewriting and therefore get rejected with the following exception message about a duplicate key :

"Internal Exception:
java.sql.SQLIntegrityConstraintViolationException: The statement was
aborted because it would have caused a duplicate key value in a unique
or primary key constraint or unique index identified by
'SQL120206135740510' defined on 'HOST'. " ( HOST is the table pointed
to by the FK).

What am I missing in the persistence.xml or else that should make the table drop before the rewrite ?
Below see the persistence.xml file ( the DB file is called 'test' . The schema is name after the user name (APP in my case) ):

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
        <persistence-unit name="jpa_test"> 
                <class>Host</class> 
                <class>Vm</class> 
                <properties> 
                        <property name="javax.persistence.jdbc.url" value="jdbc:derby://im6-64s:1527/test;create=true" /> 
                        <property name="javax.persistence.jdbc.user" value="APP" /> 
                        <property name="javax.persistence.jdbc.password" value="passw0rd" /> 
                        <!-- EclipseLink should create the database schema automatically --> 
                        <property name="eclipselink.ddl-generation" value="drop-create-tables" /> 
                        <property name="eclipselink.ddl-generation.output-mode" value="database" /> 
                </properties> 

        </persistence-unit> 
</persistence> 

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

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

发布评论

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

评论(2

狼性发作 2025-01-09 13:51:58

“drop-create-tables”应该是“drop-and-create-tables”

"drop-create-tables" should be "drop-and-create-tables"

眉黛浅 2025-01-09 13:51:58

尝试添加值为 Finest 或 ALL 的“eclipselink.logging.level”属性,以了解为什么可能无法执行删除。

Try adding the "eclipselink.logging.level" property with a value of Finest or ALL to see why the drop might not be being executed.

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