2016-02-22 150 views
1

我有以下情形:休眠@OneToMany內@Embeddable

@Entity 
class A { 
    @ElementCollection 
    private Set<B> setOfB; 
} 

@Embeddable 
class B{ 
    @OneToMany 
    private Set<C> setOfC; 
} 

@Entity 
class C{ 
    private String name; 
} 

this question看起來這是可行的,但是我收到以下錯誤。任何想法我做錯了什麼?

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: A_setOfB, for columns: [org.hibernate.mapping.Column(setOfB.setOfC)] 

謝謝。

回答

2

Hibernate根據他們的手冊在7.2.3. Collections of basic types and embeddable objects不可能。

但是,您不限於基本類型,集合類型可以是任何可嵌入的對象。要覆蓋集合表中可嵌入對象的列,請使用@AttributeOverride註釋。

@Entity 
public class User { 
    [...] 
    public String getLastname() { ...} 

    @ElementCollection 
    @CollectionTable(name="Addresses", [email protected] (name="user_id")) 
    @AttributeOverrides({ 
     @AttributeOverride(name="street1", [email protected](name="fld_street")) 
    }) 
    public Set<Address> getAddresses() { ... } 
} 

@Embeddable 
public class Address { 
    public String getStreet1() {...} 
    [...] 
} 

這樣的嵌入對象不能包含一個集合本身。