扩展实体 [英] Extending an entity

查看:96
本文介绍了扩展实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为AbstractEntity的类,该类带有@MappedSuperclass注释。然后,我有一个名为User(@Entity)的类,该类扩展了AbstractEntity。两者都存在于名为foo.bar.framework的包中。当我使用这两个类时,一切正常。但是现在我将一个包含这些文件的jar导入另一个项目。我想重复使用User类,并用其他一些字段进行扩展。我以为 @Entity公共类User扩展了foo.bar.framework.User 可以解决问题,但是我发现User的这种实现仅继承了AbstractEntity的字段。 ,但foo.bar.framework.User没有任何内容。问题是,如何让我的第二个User类继承第一个User实体类的所有字段?

I have class named AbstractEntity, which is annotated with @MappedSuperclass. Then I have a class named User (@Entity) which extends AbstractEntity. Both of these exist in a package named foo.bar.framework. When I use these two classes, everything works just fine. But now I've imported a jar containing these files to another project. I'd like to reuse the User class and expand it with a few additional fields. I thought that @Entity public class User extends foo.bar.framework.User would do the trick, but I found out that this implementation of the User only inherits the fields from AbstractEntity, but nothing from foo.bar.framework.User. The question is, how can I get my second User class to inherit all the fields from the first User entity class?

两个User类实现都使用@定义了不同的表名Table(name = name)。

Both User class implementation have different table names defined with @Table(name = "name").

我的班级是这样的



package foo.bar.framework;

@MappedSuperclass
abstract public class AbstractEntity {

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

    @Column(nullable = false)
    @Version
    protected Long consistencyVersion;

    ...
}





package foo.bar.framework;

@Entity
@Table(name = "foouser")
public class User extends AbstractEntity {

    protected String username;

    protected String password;

    ....
}





package some.application;

@Entity
@Table(name = "myappuser")
public class User extends foo.bar.framework.User {

    protected String firstname;

    protected String lastname;

    protected String email;

    ....
}

使用上面的代码,EclipseLink将创建一个名为 myappuser的表,其中包含字段 id, consistencyVersion, firstname, lastname和 email。未在表中创建用户名和密码字段-这就是我遇到的问题。

With the code above, EclipseLink will create a table named "myappuser" containing the fields "id", "consistencyVersion", "firstname", "lastname" and "email". The fields "username" and "password" are not created to the table - and that is the problem I'm having.

推荐答案

使用JPA,默认继承策略(即未指定时)为 SINGLE_TABLE :每个继承层次结构只有一个表,并且所有字段都保留在基类的表中。

With JPA, the default inheritance strategy (i.e. when not specified) is SINGLE_TABLE: there is only one table per inheritance hierarchy and all fields are persisted in the table of the base class.

如果要为继承层次结构中的每个类创建一个表,并且每个表都包含所有继承字段的列,则需要使用 TABLE_PER_CLASS 策略。

If you want to have a table for each class in the inheritance hierarchy and each table to contain columns for all inherited fields, you need to use a TABLE_PER_CLASS strategy.

package foo.bar.framework;

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
abstract public class AbstractEntity {

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

    @Column(nullable = false)
    @Version
    protected Long consistencyVersion;

    ...
}

这篇关于扩展实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆