在@Embeddable 中休眠@OneToMany

Hibernate @OneToMany inside @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)]

谢谢。

根据他们在 7.2.3. Collections of basic types and embeddable objects 的手册,在 Hibernate 中是不可能的。

But you are not limited to basic types, the collection type can be any embeddable object. To override the columns of the embeddable object in the collection table, use the @AttributeOverride annotation.

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

   @ElementCollection
   @CollectionTable(name="Addresses", joinColumns=@JoinColumn    (name="user_id"))
   @AttributeOverrides({
      @AttributeOverride(name="street1", column=@Column(name="fld_street"))
   })
   public Set<Address> getAddresses() { ... } 
}

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

Such an embeddable object cannot contains a collection itself.