在Grails 2.4中实现JPA @MappedSuperclass

时间:2014-08-29 12:06:20

标签: jpa gorm grails-2.4

我找到了this解决方案,但这并没有解决问题。在JPA中,我们可以这样做:

@MappedSuperclass
public class BasicEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date created = new Date();

    @Temporal(TemporalType.TIMESTAMP)
    private Date modified = new Date();
}

@Entity
public class User extends BasicEntity {

    private String username;
    private String password;
}

然后,hibernate.hbm2ddl.auto生成一个包含所有继承列的表,这正是我想要的:

user (
    id,
    created,
    modified,

    username,
    password,
)

在Grails中我这样做

abstract class BasicEntity {

    static mapping = {
        tablePerSubclass true
    }

    Date dateCreated
    Date lastUpdated
}

class User extends BasicEntity {

    String username
    String password
}

它会生成两个没有继承的表

basic_entity (
    id,
    version,
    date_created,
    last_updated,
)

user (
    id,
    username,
    password,
)

0 个答案:

没有答案