我一直在研究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 { }
在此先感谢您的帮助.