我有以下情况:
@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)]
感谢。
答案 0 :(得分:2)
根据他们在7.2.3. Collections of basic types and embeddable objects的手册,Hibernate无法实现。
但是您不限于基本类型,集合类型可以是任何可嵌入对象。要覆盖集合表中可嵌入对象的列,请使用@AttributeOverride批注。
@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() {...}
[...]
}
这样的可嵌入对象本身不能包含集合。