grails访问数组数据

grails访问数组数据,grails,gorm,Grails,Gorm,我无法将数据保存到我的cassandra数据库, 我犯了这个错误 这是我的代码: class User { String firstName String lastName List books static hasMany = [books:Book] } 从用户类中删除列表书籍。您已经声明有一个由static hasMany=[books:Book]创建的图书集合,我几乎可以肯定您没有发布完整的stacktrace 但让我们一步一步地检查代码并修复它:

我无法将数据保存到我的cassandra数据库, 我犯了这个错误 这是我的代码:

class User {
    String firstName
    String lastName
    List books
    static hasMany = [books:Book]

}

从用户类中删除列表书籍。您已经声明有一个由static hasMany=[books:Book]

创建的图书集合,我几乎可以肯定您没有发布完整的stacktrace

但让我们一步一步地检查代码并修复它:

    def init = { servletContext ->
        def user01= new User(firstName: "Abderrahime",lastName: "FARHANE")

        def book = new Book(title: "test")
        def book2 = new Book(title: "test2")
        //if you want to add any of these books to user01, 
        //you should first persist (save) books in database

        //but you should remember also that 
        //each instance of Book belongs to specified User
        //so author of Book can not be null
        //so first you need to save your user
        user01.save(flush: true)

        //then save your books
        book.author = user01
        book.save(flush: true)

        book2.author = user01
        book2.save(flush: true)

        //then add them (books) to user:
        user01.addToBooks(book)
        user01.addToBooks(book2)
        user01.save(flush: true)

        //now it should work:
        def user02= User.findById(user01.id)
        println(user02.books)
    }

隐马尔可夫模型。。。堆栈跟踪揭示了一个症状,但没有揭示原因。再看深一点。您也可以尝试这样保存以确保验证没有失败:user01.saveflush:true,failOnError:true。完整堆栈跟踪在哪里?通过向grails run app添加-stacktrace来获得它如果他希望它是双向的,他不需要在域类中更改任何内容?有趣的为什么它会使它双向?阅读第6.3.3节,了解级联更新和DeletesOh,等等-现在我看到了。域类用户中有不必要的列表书!我一直以为你在谈论双向关系!当我使用hibernate时,这并不能解决我的问题,但在Cassandra数据库中,它不起作用,所以我使用ListString>book,我把书的id和UUID放在一起,把用户的id放在一起。Cassandra似乎不支持hasMany和belongsTo这样的关系
class BootStrap {

        def init = { servletContext ->
            def user01= new User(firstName: "Abderrahime",lastName: "FARHANE")
            def book = new Book(title: "test")
            def book2 = new Book(title: "test2")

            user01.addToBooks(book)
            user01.addToBooks(book2)
            user01.save(flush: true)


            def user02= User.findById(user01.id)
            println(user02.books)
        }
        def destroy = {
        }
 }
| Running application...
ERROR org.springframework.boot.SpringApplication - Application startup failed
java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_72]
    at 
    def init = { servletContext ->
        def user01= new User(firstName: "Abderrahime",lastName: "FARHANE")

        def book = new Book(title: "test")
        def book2 = new Book(title: "test2")
        //if you want to add any of these books to user01, 
        //you should first persist (save) books in database

        //but you should remember also that 
        //each instance of Book belongs to specified User
        //so author of Book can not be null
        //so first you need to save your user
        user01.save(flush: true)

        //then save your books
        book.author = user01
        book.save(flush: true)

        book2.author = user01
        book2.save(flush: true)

        //then add them (books) to user:
        user01.addToBooks(book)
        user01.addToBooks(book2)
        user01.save(flush: true)

        //now it should work:
        def user02= User.findById(user01.id)
        println(user02.books)
    }