Grails / GORM 2.3 Hibernate寻找抽象域类持久化表

时间:2013-10-25 10:34:19

标签: java hibernate grails groovy gorm

我是Grails和GORM的新手,但我在旧的PostgreSQL上有一个旧的JPA2(Hibernate)应用程序来映射,但是我无法继承抽象类来实现,这里是一个问题的例子,让我们使用这个表:

users (id serial, username varchar, password char(44), user_modified integer, last_modified timestamp);
roles (id serial, role varchar, description varchar, enabled boolean, user_modified integer, last_modified timestamp);
permissions (user_id integer, role_id integer, enabled boolean);

正如您所看到的,这些表使用数字自动递增的id,但并非所有这些都是正确的,在旧的JPA映射中,我使用@MappedSuperclass来映射ID配置,例如id生成器,和一些审计专栏:

@MappedSuperclass
public abstract class DefId {
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Long id;

    @NotNull 
    @Column(name = "user_modified")
    protected Long userModified;

    @Version 
    @Column(name = "last_modified") 
    @Source(SourceType.DB)
    protected Date lastModified;

    //getters and setters
}

以下是我迄今为止在grails中尝试过的内容:

DefId.groovy:

abstract class DefId {
    /*In the actual source this are protected fields
    with public getters/setters removed for less code*/
    Long id; 
    Long userModified;
    Timestamp version;

    static mapping = {
        id generator: 'identity'
        version 'last_modified'
        userModified column: 'user_modified'
    }
}

User.groovy:

package maptest.domain

import maptest.model.DefId

class User extends DefId {
    String username
    String password
    boolean enabled
    static hasMany = [roles: Role];

    static mapping = {
        table name: "users", schema: "core"
        roles joinTable: [name: "permissions", key: "user_id"]
    }

    static constraints = {
        username blank: false, size: 2..20, matches: "^[A-Za-z]\\w+\$", unique: true
        password blank: false, matches: "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\$"
        preferences unique: true
    }
}

Role.groovy

package maptest.domain

import maptest.domain.DefId

class Role extends DefId{
    String role
    String description
    boolean enabled

    static hasMany = [users: User];
    static belongsTo = [User];

    static mapping = {
        table name: "roles", schema: "core"
        users joinTable: [name: "permissions", key: "role_id"]
    }
    static constraints = {
        role blank: false, size: 2..20, matches: "^[A-Za-z]\\w+\$", unique: true
        description   size: 2..50
    }
}

如果我将抽象类留在域结构中,我得到了异常:org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: ERROR: relation "def_id" don't exists;这是真的,但我不是试图坚持抽象类。

如果我更改包并将抽象类移动到src / groovy,那么我得到:org.springframework.orm.hibernate4.HibernateSystemException: Unknown entity: mapetest.domain.User;

我还尝试将 grails.gorm.default.mapping = {id generator:'identity'} 添加到Config.groovy并从超类中删除id字段,但这只是让我了另一个错误:Error loading plugin manager: Identity property not found, but required in domain class [maptest.domain.Role]

任何人都有任何想法?将 id生成器:​​'identity''添加到每个域类,解决它,但这会破坏继承的目的。

请原谅我的英语,这是第二语言

1 个答案:

答案 0 :(得分:1)

将您的抽象类放在domain classes文件夹中。您还需要告诉Grails每个类考虑一个不同的表:

static mapping = {
  tablePerSubclass true
}

默认只有一个包含class列的表。

您也不需要声明id字段,因为它是由Grails自动生成的,Long类型。