简体   繁体   中英

Grails/GORM 2.3 Hibernate looking for abstract domain class persisted table

I'm new to Grails and GORM, but I have an old JPA2 (Hibernate) application on a legacy PostgreSQL to map, but I can't get inheritance of abstract classes to work, here is an example of the problem, let's use this tables:

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);

As you can see the tables uses numeric auto-incremented id, but that's is not true for all of them, on the old JPA mappings I used a @MappedSuperclass to map the ID configurations, such as id generators, and some audit columns:

@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
}

Here is what I've tried so far in 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
    }
}

If I leave the abstract classes in the domain structure I got the exception: org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: ERROR: relation "def_id" don't exists; that's is true, but I'm not trying to persist the abstract classes.

If I change the package and move the abstract class to src/groovy then I got: org.springframework.orm.hibernate4.HibernateSystemException: Unknown entity: mapetest.domain.User;

I also tried to add grails.gorm.default.mapping = { id generator: 'identity' } to the Config.groovy and remove the id field from superclass, but that only got me yet another error: Error loading plugin manager: Identity property not found, but required in domain class [maptest.domain.Role]

Anyone have any idea? Adding id generator: 'identity' ´ to every single domain class, solves it but that defeats the purpose of inheritance.

Excuse my english, it's a second language

Put your abstract class in the domain classes folder. You also need to tell Grails to consider a different table per class:

static mapping = {
  tablePerSubclass true
}

The default is only one table with a class column.

You also don't need to declare the id field, since it's auto generated by Grails, with the Long type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM