我正在开发一个Spring-MVC应用程序,我正在编写一个方法来复制具有不同外键ID的Object。 所以这两个类有GroupAccount和GroupCanvas。一个GroupAccount可以拥有许多GroupCanvas' es。所以,当我说,我想要复制一个GroupCanvas,然后我想创建一个GroupCanvas的新对象,其所有值与前一个GroupCanvas中的值相同。有没有办法进行级联复制,因为GroupCanvas也有我要复制的子对象? 我发布了我的SQL代码,请好好看看:
CREATE TABLE groupaccount
(
groupid numeric NOT NULL,
groupname character varying,
adminusername character varying,
blacklist character varying[],
CONSTRAINT groupid PRIMARY KEY (groupid)
)
CREATE TABLE membercanvas
(
groupcanvasid integer NOT NULL,
mcanvasname character varying,
mcanvasnumber numeric,
mcanvastitle character varying,
mcanvasdate character varying,
mcanvascreator character varying,
mcanvasiteration character varying,
groupid numeric NOT NULL,
memberid integer NOT NULL,
mcanvasimage bytea,
sortorder numeric DEFAULT 0,
CONSTRAINT mcanvasid PRIMARY KEY (groupcanvasid),
CONSTRAINT groupaccount_membercanvas_fk FOREIGN KEY (groupid)
REFERENCES groupaccount (groupid) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT groupmembers_membercanvas_fk FOREIGN KEY (memberid)
REFERENCES groupmembers (memberid) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
CREATE TABLE membersection
(
msectionid integer NOT NULL,
msectionname character varying,
mxposition integer,
myposition integer,
groupcanvasid integer NOT NULL,
CONSTRAINT msectionid PRIMARY KEY (msectionid),
CONSTRAINT membercanvas_membersection_fk FOREIGN KEY (groupcanvasid)
REFERENCES membercanvas (groupcanvasid) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
任何帮助都会很好。非常感谢。 : - )
答案 0 :(得分:1)
通常,为此目的,您将使用copy constructor
但是,如果您需要仅在某个上下文中获取副本,而不是更好地实现服务方法并控制您复制的内容。作为相同的对象,使用spring BeanUtils
可以通过几行来获得一种方法来自文档
copyProperties(Object source, Object target)
复制属性值 将给定的源bean放入目标bean中。
和
copyProperties(Object source, Object target, String... ignoreProperties)
复制给定源bean的属性值 进入给定的目标bean,忽略给定的" ignoreProperties"。