我有 2 个班级和一个 Enum。一个类是包含枚举及其字符串表示形式的 Enum 的包装器,另一个是 User 类。枚举定义了用户的类型。它们的设置如下:
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)")
}
}
所有非常标准的东西。现在我想将用户引导到数据库中:
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 对象并准确获取它创建时使用的值?