我正在使用JPA进行mysql操作,但有几次在通过JPA执行mysql保存操作时出现错误。
执行保存操作时出现错误 =>
无法为事务打开JPA EntityManager; 重复条目的主键
表模型类:
@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);
}
}
我这里是否有遗漏的内容,如果有任何帮助将不胜感激。