gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:54:36 25 4
gpt4 key购买 nike

我正在开发一个 Spring-MVC 应用程序,其中我正在编写一个方法来复制具有不同外键 id 的对象。所以有这 2 个类 GroupAccount 和 GroupCanvas。一个 GroupAccount 可以有多个 GroupCanvas 。所以,当我说,我想复制一个 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
)

任何帮助都会很好。多谢。 :-)

最佳答案

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

但是,如果您只需要在特定上下文中获取副本,那么最好实现服务方法并控制您复制的内容。作为相同的对象,您可以通过使用 spring 的 BeanUtils 只需几行即可获得方法。

来自文档

copyProperties(Object source, Object target)

Copy the property values of the given source bean into the target bean.

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

Copy the property values of the given source bean into the given target bean, ignoring the given "ignoreProperties".

关于java - 使用不同的外键 id 复制同一类的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28538557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com