【问题标题】:Can't Clone Model.Object when ID is AUTO INCREMENTID 为 AUTO INCREMENT 时无法克隆 Model.Object
【发布时间】:2016-12-12 20:53:19
【问题描述】:

我实现了可克隆接口来克隆我的对象(复制按钮 1 或多个 Object ),但是,当模型的 Id 是自动递增时, object.save();不工作! 我在网上尝试了很多解决方案,但没有办法!

1- 在我的模型中:

@Override
public Computer clone()
{

    try
    {
        Computer nouveau = (Computer) super.clone();
        return nouveau;
    }

    catch (CloneNotSupportedException e)
    {
        e.printStackTrace();
        throw new RuntimeException();
    }
}

控制器:

public Result CopyComputers(Long selected)
{
        Computer c = Computer.find.byId(selected);
        Computer cClone = c.clone();
        Logger.debug("Object is perfectly copied and ID TOO !!!! : "+cClone);
        cClone.save(); // never works coz id is the one from primary model object.

}

我尝试了这些解决方案,但没有一个有效!!! 1-在保存之前将 id 设置为 NULL... 2-代类型如下图

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "serial")
    public Long id;

有人有想法吗?因为如果我的对象有 manytomany 或 onetomany 其他列.....我不能每次都这样做:

public Result CopyComputers(String selected)
{



        //Computer cClone = c.clone();

  String[] ids = selected.split(";");

  for (String temp : ids)
  {
        Computer c = Computer.find.byId(Long.parseLong(temp));
        try
        {

            Computer n = new Computer();
            if (c != null)
            {
            n.name = c.name;
            n.status = c.status;            
            n.introduced = c.introduced;
            n.discontinued = c.discontinued;
            n.createdt = c.createdt;
            n.createby = c.createby;
            n.moddt = c.moddt;
            n.modby = c.modby;
            n.site = c.site;
            n.company = c.company;

            n.save();
            Logger.debug("zzzzzzzz : "+n.id   +" "+c.id);
            }
            //cClone.save();

        }

        catch (Exception e)
        {
            Logger.error("Clone error", e);

        }



   Logger.info("Computer Object "+temp+" has been Copied");

   } // end for

        return GO_HOME;

}  

【问题讨论】:

    标签: java playframework clone ebean


    【解决方案1】:

    您不需要(也不应该)在 Ebean 实体 bean 上使用克隆。相反,您可以将 id 值设置为 null 并使用显式 insert() ,例如:

    Computer c = Computer.find.byId(selected);
    // null out the current id value if you are
    // inserting back into the same db
    c.setId(null); 
    
    // use explicit insert. This tells Ebean to ignore the 
    // state of the entity and forces a sql insert
    c.insert(); 
    

    请注意,您也可以使用BeanState.resetForInsert(),当插入/保存级联到您也希望复制的关联bean时,您可以这样做。

    也就是说,在调用 insert() 之前(将级联到关联的 bean),我们应该修改那些相关/关联的 bean,将它们的 id 值设置为 null 并调用 Ebean.getBeanState(otherBean).resetForInsert() ...因此这些其他相关的 bean 是也插入了。

    【讨论】:

    • 你好 rob,我正在使用 play 框架,并通过在控制器中测试你的代码(当然是 java)它说:找不到符号:setId & insert()!!!请告诉我哪个导入包可以做到这一点!!!
    • 听起来您可能使用的是旧版本的 Ebean。几年前,Ebean 从 Play 框架中接管了 Model 对象。 Ebean 的当前版本是 10.1.1(但这是全新的并且在 io.ebean 包中)。我怀疑你的依赖是 ebean 4.x ?您能否确认应用程序的当前 Ebean 版本?
    • in FOLDER : Project >> addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "3.0.1") 我必须将其更改为 10.1.1 吗?如果是,你确定整个项目不会有麻烦吗? :)
    【解决方案2】:

    是的,clone 会创建您的对象的副本,包括所有属性,但是,正如 at the Java docs 所述:

    按照惯例,这个方法返回的对象应该独立于这个对象(它正在被克隆)。为了实现这种独立性,可能需要在返回 super.clone 之前修改对象的一个​​或多个字段。

    您的save 失败,因为持久性机制试图使用现有的id 执行INSERT,然后您收到约束错误。要解决这个问题,只需执行以下操作:

    @Override
    public Computer clone() {
    
        try {
            Computer nouveau = (Computer) super.clone();
            nouveau.id = null; // reseting the id
            return nouveau;
        }
    
        catch (CloneNotSupportedException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
    

    请注意restrictions and conventions defined by clone method。

    【讨论】:

    • 嗨 MARCOS,我之前尝试过你的解决方案,但它不起作用......!!!任何其他想法,仅供参考:我正在研究游戏框架并尝试克隆对象......
    • 请添加您看到的错误。 “不工作”是什么意思?
    • 日志文件没有错误,克隆的模型不为空但保存方法无效...
    • 再说一次,不工作是什么意思?它不是保存对象吗?你是如何运行应用程序的?或者这是一个junit测试?
    • 它没有保存,因为当我记录克隆的模型时,即使将其设置为 NULL 值,它也会保留主模型(对象)的 id ......这是正常的,因为 id 不起作用是自动增量,并且永远不会保存相同的 id :) 关于在游戏框架中克隆模型对象的任何想法......谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-02-08
    • 2018-07-03
    • 2019-06-04
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    相关资源
    最近更新 更多