Grails:如何复制/克隆/复制行实例

时间:2016-04-07 17:37:20

标签: grails groovy gorm

在Grails中,有没有办法使用现有行来实例化新行?或者我是否需要遍历属性并将它们设置为这样?

def original=Musicians.get(id);

//want to start with all the same values as original and then modify
def copy=new Musicians(); 
copy.firstName="Example";
copy.lastName='Musician';

1 个答案:

答案 0 :(得分:2)

要复制原始属性,您可以使用properties属性,如下所示:

def original = Musicians.get(id)
def copy = new Musicians(original.properties)

copy.firstName = 'Example'
copy.lastName = 'Musician'

不需要分号;)