我正在寻找一种完全通过hibernate将一个实体与另一个实体关联起来的方法(没有额外的关系数据库列映射),并且不需要单独的DAO调用。我到处寻找解决方案,我唯一能找到的就是@Formula
,但是我不能让它工作。考虑一下:
@Entity
class CommonEntity {
@MagicAnnotation("maybe with HQL?")
private SuperEntity superEntity;
}
@Entity
class SuperEntity { }
这意味着有时CommonEntity
是一个SuperEntity
,我希望在POJO本身上有一个getter,这样它就可以通过一个简单的get()
访问SuperEntity
。有没有办法干净利落地做这件事,这样当我做一些像commonEntityDAO.get(1L);
这样的事情时,其中1L是一个SuperEntity,那么实体就会被设置?
数据库中的表将如下所示:
table common_entity ( common_entity_id int primary key, name string );
table super_entity ( super_entity_id int primary key, extra_field string, common_entity_id int );
发布于 2011-02-10 04:08:44
因为你有一个关系,所以这是继承的主要候选者。根据您喜欢的数据库表示,有多种继承策略。check this
发布于 2011-02-10 17:18:19
你不能简单地使用@OneToOne
吗?您的数据库架构看起来很适合它。
@Entity
class CommonEntity {
@OneToOne(mappedBy = "commonEntity")
private SuperEntity superEntity;
}
@Entity
class SuperEntity {
@OneToOne
@JoinColumn(name = "common_entity_id")
private CommonEntity commonEntity;
}
请注意,在这种情况下,您需要一个双向关系,其中SuperEntity
是拥有方,因为它持有外键。
https://stackoverflow.com/questions/4949782
复制相似问题