hibernate - 扩展域类导致奇怪的表布局和不可访问的字段

标签 hibernate grails grails-domain-class

我有一个域类用户定义为

class User extends SecUser{
    String fullName
    String address
    String city
    String country
    String role
    String email
    String schoolName
    String gradeLevel

    static constraints = {
        fullName(nullable:true)
        username(blank:false, unique:true)
        password(size:6..20, blank:false)
        //passwordRepeat(validator:)
        email(email:true, nullable:true, unique:true)
        address(nullable:true)
        city(nullable:true)
        country(nullable:true)
        phone(nullable:true) //10 digit phone number, no spaces
        role(blank:false, inList:['Teacher', 'Donor', 'Both'])
        schoolName(nullable:true)
        gradeLevel(nullable:true, inList:['K', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', 'College'])

    }
}

SecUser是通过执行spring security grails插件的quickstart脚本实现的类。其定义为
class SecUser {

    transient springSecurityService

    String username
    String password
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    static constraints = {
        username blank: false, unique: true
            password blank: false
    }
}

通常,我认为生成的数据库表将是一个USER表,上面列出的字段作为列,并且我可以使用UserController方法来实现CRUD。问题是,从不创建User表,而是通过以上字段实例化了SEC_USER表。

我的问题与创建新用户并填充相关字段有关。由于USER表不存在,因此无法从User域对象插入记录。

另一方面,我可以在SEC_USER表中插入记录,但是这些字段仅限于在SecUser域类中定义的字段。如果我确实尝试通过新的SecUser实例在“用户”的特定列中插入项目,则会收到属性未定义的错误消息。

这有点令人沮丧,因为为了完成用户注册过程,我需要访问,插入和更新相关字段,但是如何在没有访问权限的情况下执行此操作?

最佳答案

我相信您正在寻找的是每个子类映射的表,而不是每个层次结构的默认表。

默认的每层表可能会使处理数据库变得困难
另一个缺点是,列不能在数据库级别上应用“非空”约束。

在5.5.2.3继承策略中,grails对此进行了一些写作。

如果您将以下内容添加到您的域类中,则每个子类将获得一个表

 static mapping = {
    tablePerHierarchy false
 }

关于hibernate - 扩展域类导致奇怪的表布局和不可访问的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10228813/

相关文章:

java - Hibernate 未知实体(不缺少 @Entity 或导入 javax.persistence.Entity )

java - 寻找一个未弃用的 session 工厂

hibernate - java.lang.ClassNotFoundException:org.hibernate.cfg.Mappings

grails - Quartz cronExpression来自Grails中的数据库

email - 有多个发件人

java - 在调用所有 setter 添加额外字段后获取回调?

java - 无法转换为 java.io.Serializable

grails - 在 Elasticsearch Grails 中解码域类错误

grails - 使字段基于 bool 字段值且在grails域中具有多个列,使字段唯一

grails - 更新 Grails 中父域类中的 "lastUpdated"字段