Spring Boot从MySQL创建记录重复 [英] Spring boot create a duplication of record from mysql

查看:97
本文介绍了Spring Boot从MySQL创建记录重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据记录

实体在主键中给出如下

@GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id",unique = true,nullable = false)
    public Long id;

我尝试过的方法甚至使用对象映射器尝试过的方法

what i have tried and even tried with object mapper which has some other issues

Record abc =dao.findById(11);
abc.setId(Null); //not working  
dao.save(abc) //not working

所以我想做的是保存时主键为1的记录,应该另存为新记录 记录吗?

so what i am trying to do is a record with primary key as 1 when saved it should save as a new record ?

推荐答案

您应该创建一个新对象:

you should create a new object:

Record abc =dao.findById(11);
Record def = new Record(abc);
dao.save(def)

在Record类中,您应该具有这样的构造函数:

and in Record class, you should have a constructor like this:

public Record(){}
public Record(Record rec){
    this.field1 = rec.field1;
}

这篇关于Spring Boot从MySQL创建记录重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆