JPA @实体继承 [英] JPA @Entity Inheritance

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

问题描述

我现在一直在研究JPA / Hibernate @Entity 继承,似乎无法找到任何解决我试图实现的东西。



基本上我希望能够根据需要定义一个包含所有列和表映射的 @Entity 。然后,我希望能够使用不同组的 @Transient 方法扩展许多不同位置的 @Entity 在每个子实体的主体中定义。这是我尝试实现的一个基本示例,但迄今尚未取得成功:

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

公共列表< 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
}
}



<因此,任何扩展类都只会在其正文中定义 @Transient s。但是我也想允许子类为空的情况:

  @Entity 
public class MountainEntity extends MountainEntityBase实现Serializable {
}

在此先感谢您的帮助。

解决方案

使用 @Inheritance 注释在根实体上指定JPA中的继承。在那里你可以指定层次结构的数据库表示。请查阅文档了解更多详情。

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

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

Basically I want to be able to define an @Entity with all of the column and table mappings as required. Then I want to be able to extend the @Entity in a number of different locations with different sets of @Transient methods 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
@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:

@Entity
public class MountainEntity extends MountainEntityBase implements Serializable {
}

Thanks in advance for any help with this.

解决方案

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

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)

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

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