【问题标题】:Conditions for JPA OneToMany collectionJPA OneToMany 集合的条件
【发布时间】:2012-04-11 09:11:22
【问题描述】:

如果我有这个实体:

@Entity
class Pet {

    @Id
    long id;

    public enum State { ALIVE, DEAD }

    @Enumerated(EnumType.STRING)
    @...
    State state;

    @...
    String name;

}

我可以创建这样的映射吗:

@Entity
class Owner {

    @OneToMany(condition="state = ALIVE") // or something like that
    Set<Pet> alivePets;

    @OneToMany(condition="state = DEAD")
    Set<Pet> deadPets;

}

【问题讨论】:

    标签: java hibernate jpa


    【解决方案1】:

    据我所知,这不是 JPA 规范的一部分。至少 Hibernates JPA 实现提供了自己的注解@Where 可以使用:

    @OneToMany
    @Where(clause = "state = 'ALIVE'")
    Set<Pet> alivePets
    

    【讨论】:

    • 我已经在使用一些 Hibernate 特定的功能,所以没关系。
    • 我明白了,你已经改变了你的问题。再解释一下我的答案:@Where 子句是指宠物表中 state 列的 db 值。所以请检查它是否也适用于您的枚举状态。
    • 是的,我总是将枚举映射为字符串。
    猜你喜欢
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 2019-03-15
    相关资源
    最近更新 更多