java JPA @Entity 继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6170564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:39:15  来源:igfitidea点击:

JPA @Entity Inheritance

javahibernateinheritancejpapersistence

提问by travega

I have been looking into JPA/Hibernate @Entityinheritance for a while now and can't seem to find anything that addresses what I am trying to achieve.

我已经研究 JPA/Hibernate@Entity继承有一段时间了,似乎找不到任何可以解决我想要实现的目标的东西。

Basically I want to be able to define an @Entitywith all of the column and table mappings as required. Then I want to be able to extend the @Entityin a number of different locations with different sets of @Transientmethods defined in the body of each "sub-Entity". This is a basic example of what I am trying to achieve but with no success thus far:

基本上我希望能够根据需要定义@Entity所有列和表映射。然后我希望能够使用@Entity@Transient每个“子实体”的主体中定义的不同方法集在许多不同的位置扩展。这是我试图实现但迄今为止没有成功的基本示例:

@Entity
@Table(name = "mountain")
public class MountainEntityBase implements Serializable {
    public Integer mountainId = 0;
    public Integer height = 0;

    public List<ExplorerEntityBase> explorers = new ArrayList<ExplorerEntityBase>();

    @Id
    @GeneratedValue
    @Column(name = "mountain_id")
    public Integer getMountainId() { return mountainId; }
    public void setMountainId(Integer mountainId) { this.mountainId = mountainId; }

    @Column(name="height")
    public String getHeight() { return height; }
    public void setHeight(String height) { this.height = height; }

    @OneToMany(mappedBy="mountainId")
    public List<ExplorerEntityBase> getExplorers() { return this.explorers; }
    public void setExplorers(List<ExplorerEntityBase> explorers) { this.explorers = explorers; }

}    

.

.

@Entity
public class MountainEntity extends MountainEntityBase implements Serializable {

    public List<MountainEntity> allMountainsExploredBy = new ArrayList<MountainEntity>();

    @Transient
    public List<MountianEntity> getAllMountainsExploredBy(String explorerName){
        // Implementation 
    }
}

So any extended class will define only @Transients in its body. But also I want to allow for situations where the child class is empty:

因此,任何扩展类将只@Transient在其主体中定义s。但我也想允许子类为空的情况:

@Entity
public class MountainEntity extends MountainEntityBase implements Serializable {
}

Thanks in advance for any help with this.

在此先感谢您的帮助。

回答by Bozho

Inheritance in JPA is specified on the root entity using the @Inheritanceannotation. There you can specify the database representation of the hierarchy. Check the documentationfor more details.

JPA 中的继承是使用@Inheritance注释在根实体上指定的。您可以在那里指定层次结构的数据库表示。查看文档以获取更多详细信息。

If your child classes define only transient fields (not methods) (i.e. not saved in the db), then perhaps a discriminator column is the best option. But it may be the case that you don't actually need inheritance - the main entity can have all the methods (because it has all the fields the methods operate on)

如果您的子类仅定义瞬态字段(而不是方法)(即未保存在 db 中),那么鉴别器列可能是最佳选择。但可能是您实际上不需要继承的情况 - 主实体可以拥有所有方法(因为它拥有方法操作的所有字段)