2016-04-21 80 views
0

說我有列的表格(T):a1, a2, b1, b2, c1, c2休眠虛擬圖例

@Entity 
class T { 
    @ElementCollection 
    @VirtualMapKey 
    Map<String, Foo> foos 
} 

@Embeddable 
class Foo { 
    String v1 
    String v2 
} 

如果我想要得到的T實例的a1場然後我可以調用t.foos['a'].v1。我想知道是否可以通過Hibernate 5.1或JPA 2.1來實現這一點。請幫助。

回答

0

好,第一槍將返工你的實體:

@Entity 
class T { 
    @AttributeOverrides(value = { 
     @AttributeOverride(name = "v1", column = @Column(name = "a1")), 
     @AttributeOverride(name = "v2", column = @Column(name = "a2")) 
    }) 
    Foo a; 

    @AttributeOverrides(value = { 
     @AttributeOverride(name = "v1", column = @Column(name = "b1")), 
     @AttributeOverride(name = "v2", column = @Column(name = "b2")) 
    }) 
    Foo b; 


    @AttributeOverrides(value = { 
     @AttributeOverride(name = "v1", column = @Column(name = "c1")), 
     @AttributeOverride(name = "v2", column = @Column(name = "c2")) 
    }) 
    Foo c; 
} 

然後你就可以在你的表做簡單t.a.v1, t.a.v2, t.b.v1, t.b.v2等你有固定的列數 - 只是反映它的映射。