java - 实体管理器中出现重复键错误,并保留正确的对象

标签 java jpa eclipselink derby entitymanager

我有以下实体 TitleScreenFormat ,当我执行 entitymanager.commit 时 我收到以下错误

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 'SQL130418144233630' defined on 'S1_TITLESCREENFORMAT'. Error Code:
20000 Call: INSERT INTO s1_TitleScreenFormat
(titlescreenformat_titleid, titlescreenformat_format,
titlescreenformat_deliveryformat, TITLE_title_id) VALUES (?, ?, ?, ?)
    bind => [TitleId1, Format1, DeliveryFormat1, null]

这是带有键的实体

@Entity
    @Table(name = "s1_TitleScreenFormat")
    public class TitleScreenFormat 
    {
        @Id 
        @Column(name = "titlescreenformat_titleid", length = 128)    
        private String TitleId;

        @Id 
        @Column(name = "titlescreenformat_deliveryformat", length = 128)    
        private String DeliveryFormat;

        @Id 
        @Column(name = "titlescreenformat_format", length = 128)    
        private String Format;


        private Title Title;

这是我想要保留的对象的实例

[0] TitleScreenFormat  (id=81)  
    DeliveryFormat  "DeliveryFormat1" (id=87)   
    Format  "Format1" (id=88)   
    Title   null    
    TitleId "TitleId1" (id=89)  
[1] TitleScreenFormat  (id=83)  
    DeliveryFormat  "DeliveryFormat2" (id=90)   
    Format  "Format2" (id=91)   
    Title   null    
    TitleId "TitleId2" (id=92)  
[2] TitleScreenFormat  (id=84)  
    DeliveryFormat  "DeliveryFormat3" (id=93)   
    Format  "Format3" (id=94)   
    Title   null    
    TitleId "TitleId3" (id=95)  
[3] TitleScreenFormat  (id=85)  
    DeliveryFormat  "DeliveryFormat4" (id=96)   
    Format  "Format4" (id=97)   
    Title   null    
    TitleId "TitleId4" (id=98)  
[4] TitleScreenFormat  (id=86)  
    DeliveryFormat  "DeliveryFormat5" (id=99)   
    Format  "Format5" (id=100)  
    Title   null    
    TitleId "TitleId5" (id=101)

在 persistence.xml 文件中我使用以下配置

<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />

我正在使用 Derby DB。

此错误的原因可能是什么?

最佳答案

我认为您在数据库中再次插入相同的值。 在保留这些值之前,只需在数据库中检查一次(如果它们已经存在)。

或者,您可以更改这些值并尝试再次提交。

关于java - 实体管理器中出现重复键错误,并保留正确的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16099916/

相关文章:

java - 尽管另有配置,Eclipselink 何时使用 DeferredChangeDetectionPolicy?

jpa - eclipselink 查询结果缓存不起作用

java - 我必须打印出这个方程的 5 个 future 值

java - 如何在 JPA 中使用 Postgres JSONB 数据类型?

java - 当尝试使用 eclipse juno 服务器运行 struts2 时,出现 HTTP Status 404 错误

java - 连接非实体类中的两个实体

java - JPA 查询.getResultList()?

java - JPA OneToMany 列表找不到应该继承的 mappedBy 属性

java - xtext生成器如何防止文件覆盖

java - 多态不就是封装的一个效果吗?