【问题标题】:eclipse link duplicate keys strange erroreclipse链接重复键奇怪的错误
【发布时间】:2025-12-05 02:55:01
【问题描述】:

当我为第一个实体(customer1)创建数据时,数据已成功创建 但是当我想为第二个实体创建数据时,我收到以下错误。 我们在 两个不同的包 中使用不同的表注释名称生成这两个实体,所以我不明白为什么我会收到此错误。 我们还为每个实体创建不同的持久化单元名称... 这个错误的原因可能是什么?

错误在提交中

entityManager.getTransaction().commit();
        entityManager.close();

异常 [EclipseLink-4002](Eclipse 持久性服务 - 2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions.DatabaseException 内部 异常:java.sql.SQLIntegrityConstraintViolationException: 语句被中止,因为它会导致重复键 标识的唯一或主键约束或唯一索引中的值 由在“customer02”上定义的“SQL130407132754180”。错误代码:20000 呼叫:INSERT INTO customer02(CUSTOMER, NOTE, BUYERID) VALUES (?, ?, ?) bind => [3个参数绑定]查询: InsertObjectQuery(prod2.customer02@7fe4bdcc)

包裹中的实体 1 客户 ...customer01

package prod1;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="customer01")
public class Customer
{

  @Id
  @Column(name="customer_number")
  private String customer;
  private String Note;
  private String BuyerId;

包裹中的实体 2 客户 ...customer02

package prod2;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="customer02")
public class Customer
{

  @Id
  @Column(name="customer_number")
  private String customer;
  private String Note;
  private String BuyerId;

持久化单元是

-<persistence-unit name="prod1">
<class>prod1.customer01</class>-
<properties><property name="eclipselink.ddl-generation" 
value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="SEVERE"/>
</properties></persistence-unit>-

-<persistence-unit name="prod2">
<class>prod2.customer02</class>-
<properties><property name="eclipselink.ddl-generation" 
value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="SEVERE"/>
</properties></persistence-unit>-

【问题讨论】:

  • IMO,这不是 Eclipse 链接问题,因为 Eclipse 链接似乎只是传递了一个 SQL 异常。你能显示数据库约束SQL130407132754180吗?您确定 customer02 中没有任何重复吗?
  • no there is no dumps in customer02 我已经调试了代码,如何显示数据库约束?我如何找到它?
  • 你使用哪个数据库?
  • Hana 是 SAP 数据库
  • 这似乎没有使用排序,那么您的ID值是如何分配的?您的应用必须设置它们并负责挑选独特的。

标签: java database jpa eclipselink


【解决方案1】:

显示的注释没有为 ID 定义排序。尝试添加 @GeneratedValue 让提供商为您分配它们。

http://wiki.eclipse.org/EclipseLink/Examples/JPA/PrimaryKey

【讨论】:

  • 感谢您的回答,但是如果例如实体 1 的 ID 为 1-10,我应该如何管理它,当我去生成的实体 2 时,我应该如何知道 ID,如果我不知道他们我猜猜我会得到同样的错误......
  • 如果您让提供程序处理排序,它将按顺序分配值,并递增计数器,以免重新分配