开发者

How to populate a List in the domain class in grails?

开发者 https://www.devze.com 2023-03-08 08:48 出处:网络
Hi folks I have a map in my bootstrap.groovy, how can use this map to populate another map in my domain class?

Hi folks I have a map in my bootstrap.groovy, how can use this map to populate another map in my domain class? here is the code

bootstrap.groovy

开发者_StackOverflow
def data = [ x:'45.5',
             y:'7',
             z:[ z0:'2.5', z1:'3.5', z2:'4.0', z3:'3.5', z4:'5.0']
           ]

what code should I write here?

//some code

domain class

class target implements Serializable{
  //Just for the time being
  List list = new ArrayList()
}

any ideas would be appreciated


First thing is your question asks how to populate one map with another map, but you define a list in your domain. So if I'm understanding you correctly, your domain would more likely be:

class Target implements Serializable {
    Map data = [:]
}

// BootStrap.groovy
import package.name.Target
class BootStrap {

    def grailsApplication

    def init = { servletContext ->

        // no need to quote your map keys in this case
        Map data = [x:"45.5", y:"7", z:[z0 :"2.5", z1:"3.5", z2:"4.0", z3:"3.5", z4:"5.0"]

        Target targ = new Target()
        targ.data = data.z // set Target data map to nested portion of map above
        targ.data = data // set equal (could add to ctor [data:"$data"] instead)
        data.each{k,v->
            // do some calc that changes local map values and applies to target data map
        }

        // if you are unable to get a reference to Target domain, you can try
        def inst = grailsApplication.getClassForName("package.name.Target").newInstance()
        inst.data = data
        // etc.
    }
}


I believe it's better to use Grails configuration and update Config.groovy.

0

精彩评论

暂无评论...
验证码 换一张
取 消