讓我們忘了這裏的示例(這可能不是最好的)。使用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