Search code examples
grailsgrails3

How to use generated grails 3 service in loop to store multiple entries in database


I might be asking childish question because I am newbie in grails3.

I want to store multiple entries in database and I am using all default configuration. This is my small code. I have injected service and using save method of this service in for loop.

MyService myService

for (int i = 0; i < 2; i++){
    // some code modification

    myService.save(domainClass)
}

But this code store only last entries. Let me know if I am missing something.


Solution

  • It's because java sent values to method by link(not by value*), so that you update your domainClass every time. In your case you need to either make new DomainClass(domainClass.properties) or the same but with params.

    *Exclude primitives