2011-02-09 94 views
0

我正在寻找一种方法来一个实体与另一个实体纯粹通过Hibernate关联(没有多余的关系数据库列映射),而不是需要单独的DAO调用。我搜索周围的解决方案,我能找到的唯一的事情就是@Formula 但我无法得到它的工作。试想一下:休眠:虚拟列

@Entity 
class CommonEntity { 
    @MagicAnnotation("maybe with HQL?") 
    private SuperEntity superEntity; 
} 

@Entity 
class SuperEntity { } 

这意味着,有时CommonEntitySuperEntity,我想对POJO本身吸气所以它具有通过简单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); 
+0

你说CommonEntity是阿SuperEntity但在你的例子CommonEntity有SuperEntity。 – adrianboimvaser 2011-02-09 20:06:57

回答

0

因为你有一个关系,这是继承的主要候选。根据您喜欢的数据库表示形式,有多种继承策略。 check this

0

不能单纯使用@OneToOne?您的数据库模式看起来适合它。

@Entity 
class CommonEntity { 
    @OneToOne(mappedBy = "commonEntity") 
    private SuperEntity superEntity; 
} 

@Entity 
class SuperEntity { 
    @OneToOne 
    @JoinColumn(name = "common_entity_id") 
    private CommonEntity commonEntity; 
} 

注意,在这种情况下,你需要SuperEntity作为一个拥有方的双向关系,因为它拥有的外键。