有没有最好的方法来克隆模型以仅更改一个条目?

发布于 2024-11-10 15:29:38 字数 974 浏览 7 评论 0原文

我有一个包含一些字段的模型,我想在其中添加一个新条目 该模型的数据库,但仅更改一个字段。有没有 这样做的最佳方法,无需创建新实例 每个字段一一设置?

案例:

public class MyModel extends Model {
    public String firstname;
    public String lastname;
    public String city;
    public String country;
    public Integer age;

}

我实际上拥有的代码

MyModel user1 = MyModel.findById(1);
MyModel user2 = new MyModel();

// is there a way to do it with clone or user1.id = null ? and then create()?
// Actually, I do that :

user2.firstname = "John";
user2.lastname = user1.lastname;
user2.city = user1.city;
user2.country = user1.country;
user2.age = user1.age;
user2.create();

我正在寻找的代码会执行类似以下操作:

MyModel user1 = MyModel.findById(1);
MyModel user2 = clone user1;
user2.firstname = "John";
user2.create();

MyModel user = MyModel.findById(1);
user.id = null;
user.firstname = "John";
user.create(); 

但我不知道这样做是否正确。

I have a model with some fields, and I'd like to add a new entry in
the database of this model, but with changing only one field. Is there
a best way to do so, without having to create a new instance and
setting one by one each field ?

Case :

public class MyModel extends Model {
    public String firstname;
    public String lastname;
    public String city;
    public String country;
    public Integer age;

}

And the code I actually have

MyModel user1 = MyModel.findById(1);
MyModel user2 = new MyModel();

// is there a way to do it with clone or user1.id = null ? and then create()?
// Actually, I do that :

user2.firstname = "John";
user2.lastname = user1.lastname;
user2.city = user1.city;
user2.country = user1.country;
user2.age = user1.age;
user2.create();

What I am lookig for would to do something like :

MyModel user1 = MyModel.findById(1);
MyModel user2 = clone user1;
user2.firstname = "John";
user2.create();

or

MyModel user = MyModel.findById(1);
user.id = null;
user.firstname = "John";
user.create(); 

But I don't know if it's correct to do it like that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2024-11-17 15:29:38

为实体实现Cloneable接口,&调用clone()方法将返回原始对象的浅表副本。要获取深拷贝,请重写它,您可以将 id 设置为 null &复制非原始字段。

@Override
protected Object clone() throws CloneNotSupportedException {

        MyModel model = (MyModel) super.clone();        
        model.setId(null);

        //-- Other fields to be altered, copying composite objects if any

        return model.   
}

保留克隆的对象:

MyModel user = MyModel.findById(1);
detachedUser = user.clone(); //-- cloning
user.firstname = "John"; //-- modifying
user.create(); //-- persisting

Implement the Cloneable interface for the entity, & than calling clone() method will return a shallow copy of the original object. To obtain a deep copy, override it, where you can set id as null & copy non-primitive fields.

@Override
protected Object clone() throws CloneNotSupportedException {

        MyModel model = (MyModel) super.clone();        
        model.setId(null);

        //-- Other fields to be altered, copying composite objects if any

        return model.   
}

Persisting the cloned object :

MyModel user = MyModel.findById(1);
detachedUser = user.clone(); //-- cloning
user.firstname = "John"; //-- modifying
user.create(); //-- persisting
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文