【发布时间】:2019-04-04 08:59:39
【问题描述】:
我正在使用 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
}
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`)
)
存储库类:
@Repository
public interface TableXRepository extends CrudRepository<tableX,Long> {
}
Mysql操作类:
@Component
@Transactional
public class tableXoperationImpl implements ItableXoperation {
@Autowired
private TableXRepository tableXRepository;
public void save(tableX data) {
tableXRepository.save(data);
}
}
是什么,我在这里失踪,任何帮助将不胜感激。
【问题讨论】:
-
您能否添加更多日志文件,是否有测试源代码或调用 ITableX 操作接口的客户端代码。作为一点,java中的类名应该是小写的。
-
你用来执行保存操作的数据是什么。你会使用重复的
product_id -
@IsharaMadhawa 我认为,如果您使用相同的 product_id 保存文档,如果文档已经存在,jpa 将更新代替插入..
标签: mysql spring-boot jpa spring-data-jpa