0
私は春のセキュリティを使用しており、追加のドメインを追加しています - ユーザが作成されたときに作成されない "UserSettings"。Grails 1対1の関係でクライアントを作成
すべての原則に従うために、ここでは一対一の関係を作成することにしました。
ユーザードメイン:
package com.trading.security
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import com.trading.portal.UserSettings
@EqualsAndHashCode(includes='username')
@ToString(includes='username', includeNames=true, includePackage=false)
class User implements Serializable {
private static final long serialVersionUID = 1
transient springSecurityService
String username
String password
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
Set<Role> getAuthorities() {
UserRole.findAllByUser(this)*.role
}
def beforeInsert() {
encodePassword()
us = new UserSettings(user:this,currency:'SEK', volumeUnit: 'AM3').save(failOnError:true)
}
def afterInsert() {
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
UserSettings getUserSettings() {
us
}
protected void encodePassword() {
password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
}
static transients = ['springSecurityService']
static constraints = {
password blank: false, password: true
username blank: false, unique: true
us unique:true, nullable:true
}
static mapping = {
password column: '`password`'
table '`user`'
}
static hasOne = [us:UserSettings]
}
とUserSettingsドメイン:
package com.trading.portal
import com.trading.security.User
class UserSettings {
int id
int SupplierId
String supplierName
String currency
String volumeUnit
static belongsTo = [user : User]
static mapping = {
id column: 'id', type: 'integer'
}
static constraints = {
user()
supplierName()
currency(inList: ['SEK', 'EUR', 'USD', 'GBP'])
volumeUnit(inList: ['AM3', 'PKG', 'AM1', 'AM2'])
supplierId nullable:true
currency nullable:true
volumeUnit nullable:true
supplierName nullable:true
}
}
だから私は、ユーザーのドメインの前に挿入手順でusersettingsを作成しようとしましたが、メッセージで失敗しています:
org.springframework.dao.InvalidDataAccessApiUsageException:
Not-null property references a transient value - transient instance must be saved before current operation :
com.trading.portal.UserSettings.user -> com.trading.security.User;
nested exception is org.hibernate.TransientPropertyValueException:
Not-null property references a transient value - transient instance must be saved before current operation :
com.trading.portal.UserSettings.user -> com.trading.security.User
どうしたのですか?これを解決するために私は何ができますか?
これは、Userの作成時にUserSettingsの自動作成を行うことができないことを意味しますか?私はこれのようにする必要がありますか? u =新しいユーザー(...)。save(); u.us =新しいUserSetting(...) – larand