我将数据保存到我的cassandra数据库时出现问题, 我收到了这个错误 还有我的代码:
class User {
String firstName
String lastName
List books
static hasMany = [books:Book]
}
class Book {
String title
static belongsTo = [author : User]
}
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
答案 0 :(得分:-1)
从List books
课程中删除User
。您已经通过static hasMany = [books:Book]
答案 1 :(得分:-1)
我几乎可以肯定你没有发布完整的堆栈跟踪。
但是,让我们一步一步检查你的代码并修复它:
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)
}