小编Ami*_*mar的帖子

JPA/Spring-boot 密钥“PRIMARY”的重复条目

我使用 JPA 进行 mysql 操作,但有几次在通过 JPA 执行 mysql 保存操作时遇到错误。执行保存操作时出错=>

无法打开 JPA EntityManager 进行事务;密钥“PRIMARY”重复输入

表模型类:

@Entity
@Table(name="table_x")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@TypeDefs({
        @TypeDef(name = "json", typeClass = JsonStringType.class),
        @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class),
})
public class tableX implements Serializable {

    @Id
    @Column(name="product_id")
    private Long productId;

    @Column(name="parent_id")
    private Long parentId;

    // other fields
}
Run Code Online (Sandbox Code Playgroud)

Mysql 架构:

 CREATE TABLE `table_x` (
  `product_id` int(12) unsigned NOT NULL,
  `parent_id` int(12) unsigned DEFAULT NULL,
  // other fields
  PRIMARY KEY (`product_id`)
)
Run Code Online (Sandbox Code Playgroud)

存储库类:

@Repository
public …
Run Code Online (Sandbox Code Playgroud)

mysql jpa spring-data-jpa spring-boot

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

jpa ×1

mysql ×1

spring-boot ×1

spring-data-jpa ×1