Grails 如何使用 belongsTo 关联将数据保存在 table 中?
Grails How to save data in a table with belongsTo association?
我有两个模型类:用户和余额。
class User {
Balance balance
String name
String role
String password
static constraints = { }
}
class Balance {
double balance
static belongsTo = [user:User]
static constraints = {}
}
每一笔余额都属于一个用户。现在,我需要使用 saveAdd() 函数将用户和余额详细信息保存在各自的表中。
def saveAdd(){
def name = params.name
def password= params.password
def balance = Double.parseDouble(params.balance)
def u = new User(name:name,password:password,role:"user", balance: new Balance(balance:balance))
u.save(flush:true)
}
但是我收到 "java.lang.reflect.InvocationTargetException: null" 错误。
映射过程中是否有错误?我错过了什么?
我会重新组织代码:
class User {
static hasOne = [ balance:Balance ]
}
class Balance {
static belongsTo = [user:User]
}
那么您在控制器中保存对象应该没有问题:
def saveAdd(){
def u = new User(...)
def b = new Balance(user:u, ...)
b.save()
u.save(flush:true)
}
我有两个模型类:用户和余额。
class User {
Balance balance
String name
String role
String password
static constraints = { }
}
class Balance {
double balance
static belongsTo = [user:User]
static constraints = {}
}
每一笔余额都属于一个用户。现在,我需要使用 saveAdd() 函数将用户和余额详细信息保存在各自的表中。
def saveAdd(){
def name = params.name
def password= params.password
def balance = Double.parseDouble(params.balance)
def u = new User(name:name,password:password,role:"user", balance: new Balance(balance:balance))
u.save(flush:true)
}
但是我收到 "java.lang.reflect.InvocationTargetException: null" 错误。 映射过程中是否有错误?我错过了什么?
我会重新组织代码:
class User {
static hasOne = [ balance:Balance ]
}
class Balance {
static belongsTo = [user:User]
}
那么您在控制器中保存对象应该没有问题:
def saveAdd(){
def u = new User(...)
def b = new Balance(user:u, ...)
b.save()
u.save(flush:true)
}