JPA 2.0通過@ElementCollection注釋可以支持原語集合,您可以將其與java.util.Map集合支持一起使用。這樣的事情應該起作用:
@Entity
public class Example {
@Id long id;
// ....
@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable(name="example_attributes", joinColumns=@JoinColumn(name="example_id"))
Map attributes = new HashMap(); // maps from attribute name to value
}
另請參見(在JPA 2.0規范中)
2.6-可嵌入類和基本類型的集合
2.7地圖集
10.1.11-ElementCollection批注
11.1.29 MapKeyColumn注釋