【问题标题】:Persisting an Entity in derby DB Using EntityManager gives java.sql.SQLIntegrityConstraintViolationException使用 EntityManager 在 derby DB 中持久化实体会产生 java.sql.SQLIntegrityConstraintViolationException
【发布时间】:2015-03-18 14:19:37
【问题描述】:

当我尝试在 Derby DB 中使用 Entitymanger 持久化对象时出现以下错误:-

 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 'SQL150318192139760' defined on 'TESTSEARCHS '.
    Error Code: 20000
    Call: INSERT INTO TESTSEARCHS (SEARCHID) VALUES (?)
        bind => [1 parameter bound]
    Query: InsertObjectQuery(example.com.TestSearchs@4e049f)

相关代码是:-

  public class TestA extends SuperA{
            @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
            protected TestSearchs testSearchs;
    ........
        }

SuperA 类为 TestA 提供 ID

  @Entity
  @XmlTransient
  @MappedSuperclass
  @Inheritance(strategy = InheritanceType.JOINED)
  public class SuperA{
      @Id
      private String id;
    .........

    }

TestSearchs 类是:-

public class TestSearchs {

    @Id
    @GeneratedValue
    @XmlTransient
    private long searchId;

    @ElementCollection(targetClass = String.class,fetch = FetchType.EAGER)
    protected List<String> testsearch=new ArrayList<String>();

.....

}

我通过调用唯一的 TestA 的 setter 来设置 Id,然后我使用 entityManager.Persit() 持久化 TestA 对象,但出现以下错误。请告诉我我做错了什么?

【问题讨论】:

    标签: java sql jpa derby


    【解决方案1】:

    这有帮助吗(在 TestSearchs 上)?

    @GeneratedValue(strategy = GenerationType.TABLE)
    

    【讨论】:

      猜你喜欢
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-08
      • 2018-05-20
      相关资源
      最近更新 更多