问题
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