【发布时间】: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