java – JPA @Entity继承
发布时间:2020-05-28 12:16:05 所属栏目:Java 来源:互联网
导读:我一直在研究JPA / Hibernate @Entity继承一段时间,似乎无法找到解决我想要实现的问题的任何东西. 基本上我希望能够根据需要定义一个包含所有列和表映射的@Entity.然后,我希望能够使用在每个“子实体”的主体中定义的不同@Transient方法集在多个不同位置扩展@
|
我一直在研究JPA / Hibernate @Entity继承一段时间,似乎无法找到解决我想要实现的问题的任何东西. 基本上我希望能够根据需要定义一个包含所有列和表映射的@Entity.然后,我希望能够使用在每个“子实体”的主体中定义的不同@Transient方法集在多个不同位置扩展@Entity.这是我想要实现的基本示例,但到目前为止没有成功: @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
}
}
因此任何扩展类都只在其体内定义@Transients.但是我也希望允许子类为空的情况: @Entity
public class MountainEntity extends MountainEntityBase implements Serializable {
}
在此先感谢您的帮助. 解决方法JPA中的继承是使用@Inheritance注释在根实体上指定的.在那里,您可以指定层次结构的数据库表示.检查 the documentation以获取更多详细信息.如果您的子类只定义了瞬态字段(而不是方法)(即没有保存在数据库中),那么鉴别器列可能是最佳选择.但实际情况可能是您实际上不需要继承 – 主实体可以拥有所有方法(因为它具有方法操作的所有字段) (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
