使用不同的外键id复制同一类的对象

时间:2015-02-16 09:48:06

标签: java spring hibernate spring-mvc

我正在开发一个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
)

任何帮助都会很好。非常感谢。 : - )

1 个答案:

答案 0 :(得分:1)

通常,为此目的,您将使用copy constructor

但是,如果您需要仅在某个上下文中获取副本,而不是更好地实现服务方法并控制您复制的内容。作为相同的对象,使用spring BeanUtils

可以通过几行来获得一种方法

来自文档

copyProperties(Object source, Object target)
  

复制属性值   将给定的源bean放入目标bean中。

copyProperties(Object source, Object target, String... ignoreProperties) 
  

复制给定源bean的属性值   进入给定的目标bean,忽略给定的" ignoreProperties"。