Hibernate:@wherejointable来过滤子表

时间:2011-05-20 09:46:52

标签: hibernate annotations where-clause

我已经在使用@where子句来过滤实体的软删除行。 (带有'已删除'标志)。这非常有效。

现在,我需要过滤存储链接项目历史的链接表。 历史策略,只是使用链接表的max id行。 这就是我现在所拥有的:

@Where(clause="deleted = 0" )
EntityA{

   @Column(name = "id")
   Integer id;

   @Column(name = "deleted")
   Integer deleted;

   @OneToMany(mappedBy = "a")
   List<EntityB> subEntities;
   EntityB getCurrentEntityB(){
       .. loop in java that return the B with the max(id).
   }
}

EntityB{

    @Column(name = "id")
    Integer id;
    @JoinColumn(name = "A_ID")
    EntityA a;
}

我认为我可以使用@WhereJoinTable注释简化这一点,但我对可能性感到有些失落。 我试过这样的事情:

@Where(clause="deleted = 0" )
EntityA{
   ...
   @OneToOne(mappedBy = "a")
   @WhereJoinTable(clause = "id in( select max(id) from entityB where a_id = :id )")
   EntityB currentEntityB;
}

但语法不正确,我真的不知道怎么做......

在网上找到关于此功能的大量文档并不容易......

任何帮助?

学家

0 个答案:

没有答案