让我们忘了这里的示例(这可能不是最好的)。使用Hibernate,可以映射基本类型或与@CollectionOfElements
注释embeddedable对象的集合(和可选的@IndexColumn
有序集合):
@Entity
public class Product {
@Id @GeneratedValue
private Long id;
@CollectionOfElements @IndexColumn(name="price_index")
private List<Integer> prices = new ArrayList<Integer>();
...
}
语义上说,这是接近@OneToMany
除了集合中的元素不是实体,它们没有id属性,它们的生命周期完全依赖于所有者对象。
从一个数据库来看,这将导致在一个表中的产品和的价格表:
create table Product (id bigint not null, primary key (id))
create table Product_prices (Product_id bigint not null, element integer, price_index integer not null, primary key (Product_id, price_index))
alter table Prodcut_prices add constraint FK9D26D06FB343359D foreign key (Product_id) references Product
在JPA 2.0,这个注释已经标准化,从而更喜欢新@ElementCollection
注释如果您使用JPA 2.0。
这就是说,对于产品和价格的特殊情况,@duffymo所说的是非常真实的,他们可能不应该使用提到的注释来实现。
好的,谢谢,你说的都很有道理。 – Pin 2010-05-27 09:45:13