私は2つのクラスとEnumを持っています。1 つのクラスは、列挙型とその文字列表現の両方を含む列挙型のラッパーであり、もう 1 つはユーザー クラスです。列挙型は、ユーザーのタイプを定義します。それらは次のように設定されます。
Class User {
String username
String password
String firstName
String lastName
String emailAddress
UserStatus status
UserType type
Date dateCreated
List<UserRole> roles
static mapping = {
roles(fetch: 'join')
}
}
私の列挙型は次のとおりです。
enum RoleType {
SUPER_USER('superuser','Super User'),
SUPPORT_USER('supportuser','Support User'),
STANDARD('standard','Standard')
String id
String name
RoleType(String id, String name) {
this.id = id
this.name = name
}
String toString() {
name
}
}
そして私のラッパークラスは次のとおりです:
class UserRole {
static belongsTo = [user:User]
static auditable = true
RoleType roleType
String role
static constraints = {
role(nullable:false, blank:false)
roleType(nullable:false, inList:RoleType.values().toList())
}
static mapping = {
sort(role: "asc")
role(role: IdentityEnumType,sqlType: "varchar(40)")
}
}
すべてのかなり標準的なもの。ここで、ユーザーを DB にブートストラップします。
User user = new User(
username:'support@quirk.biz',
password:'ilovequirk',
status:UserStatus.ACTIVE,
type:UserType.QUIRK,
firstName:'Quirk',
lastName:'Support',
emailAddress:'support@quirk.biz'
)
UserRole userRole = new UserRole(roleType:RoleType.SUPER_USER, role:RoleType.SUPER_USER.toString())
user.addToRoles(userRole)
user.save(failOnError:true)
addToRoles 行にヒットすると、壊れてエラーメッセージが表示されます。
No signature of method: za.co.hollard.User.addToRoles() is applicable for argument types: (za.co.hollard.UserRole) values: [za.co.hollard.UserRole : null]
しかし、addToRoles メソッドの前にいくつかの printlns を投入すると、新しく作成された UserRole オブジェクトを調べて、作成時の値を正確に取得できますか??