Upgrade issue to Grails 2.4.4

蹲街弑〆低调 提交于 2019-12-04 04:39:34

Finding out what's wrong

The first thing you need to do is to find out which field(s) cause the issue. Likely it'll be any field in a domain class declared as a List.

In my case it was easy to find them, because the project is on very early stage and there is not too much domains.

However, I found a possible solution to narrow down the possible culprits set.

The interesting part is:

What sucks is the only good way to figure out where they are is to set a breakpoint on line 436 of AbstractGrailsDomainBinder and look at the state of affairs

Fixing the issue

When you find the improper fields, it's time to implement a workaround.

Let's assume that our culprit was List authors in a domain class like:

class Book {
   List<Integer> authors // keeps author ids
} 

We need to get rid of the list, of course, so the solution would be something like:

class Book {

    static transients = ['authors']

    String authorIds

    public void setAuthors(List<Integer> authorList) {
        this.authorIds = authorList ? authorList.join(";") : ''
    }

    public List<Integer> getAuthors() {
        return authorIds?.split(";")?.collect { it.toInteger() } ?: []
    }

} 

Possible side effect

I noticed that the setter needs to be called explicitly to work.

I'm quite sure that in previous Grails version the following code would call the setter:

new Book(authors: [1, 2, 3])

But it looks like in Grails 2.4.4 it needs to be done like:

def book = new Book()
book.setAuthors([1, 2, 3])
splatterfreak

You also need to specify the hasMany map. It seems that it is not more sufficient to just add List<DomainClassName> listName in your Domain-Class

you also need to add

static hasMany = [
        listName: DomainClassName
]

at least it works for me.

Try to replace problematic List interface with something that is real class, for example ArrayList

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!