List of Strings in a Grails domain class

故事扮演 提交于 2019-12-10 10:45:03

问题


I am trying to have a List of String that works in MySql in a Grails domain class.

I have tried the following:

class Catalogue {

List books
String book
static hasMany = [books: book]

}

and

class Catalogue {

List books

}

and

class Catalogue {

String[] books

}

and

class Catalogue {

ArrayList<String> books = new ArrayList<String>()

}

The last three compiles but the entry is not present in MySQL. There is no table, or column to represent this data in MySQL and I have tried populating the array with data. Still nothing.

Any ideas?


回答1:


You can achieve this by using hasMany. Furthermore you need to define books as a List since without it you would get a simple Set that does not allow any duplicates.

class Catalogue {
    static hasMany = [books: String]
    List books
}



回答2:


Create a domain Book class:

class Book {
 String title
 String isbn
 ...

String toString(){
  return "${title}" 
 }

}

and modify your Catalogue as

class Catalogue{
  String name
  ....
  static hasMany = [books: Book]
  //etc etc 
}


来源:https://stackoverflow.com/questions/24672310/list-of-strings-in-a-grails-domain-class

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