JavaPersistenceWithHibernate第二版笔记-第七章-003Mapping an identifier bag(@OrderColumn、@ElementCollection、@CollectionTable、、)

一、结构

二、代码

1.

 1 package org.jpwh.model.collections.listofstrings;
 2 
 3 import org.jpwh.model.Constants;
 4 
 5 import javax.persistence.CollectionTable;
 6 import javax.persistence.Column;
 7 import javax.persistence.ElementCollection;
 8 import javax.persistence.Entity;
 9 import javax.persistence.GeneratedValue;
10 import javax.persistence.Id;
11 import javax.persistence.OrderColumn;
12 import java.util.ArrayList;
13 import java.util.List;
14 
15 @Entity
16 public class Item {
17 
18     @Id
19     @GeneratedValue(generator = Constants.ID_GENERATOR)
20     protected Long id;
21 
22     @ElementCollection
23     @CollectionTable(name = "IMAGE")
24     @OrderColumn // Enables persistent order, Defaults to IMAGES_ORDER
25     @Column(name = "FILENAME")
26     protected List<String> images = new ArrayList<String>();
27 
28     public Long getId() {
29         return id;
30     }
31 
32     public List<String> getImages() {
33         return images;
34     }
35 
36     public void setImages(List<String> images) {
37         this.images = images;
38     }
39 
40     // ...
41 }

 

2.

 1 package org.jpwh.test.collections;
 2 
 3 import org.jpwh.env.JPATest;
 4 import org.jpwh.model.collections.listofstrings.Item;
 5 import org.testng.annotations.BeforeClass;
 6 import org.testng.annotations.Test;
 7 
 8 import javax.persistence.EntityManager;
 9 import javax.transaction.UserTransaction;
10 
11 import static org.testng.Assert.assertEquals;
12 
13 public class ListOfStrings extends JPATest {
14 
15     @Override
16     public void configurePersistenceUnit() throws Exception {
17         configurePersistenceUnit("ListOfStringsPU");
18     }
19 
20     @Test
21     public void storeLoadCollection() throws Exception {
22         UserTransaction tx = TM.getUserTransaction();
23         try {
24             tx.begin();
25             EntityManager em = JPA.createEntityManager();
26             Item someItem = new Item();
27 
28             someItem.getImages().add("foo.jpg");
29             someItem.getImages().add("bar.jpg");
30             someItem.getImages().add("baz.jpg");
31             someItem.getImages().add("baz.jpg");
32 
33             em.persist(someItem);
34             tx.commit();
35             em.close();
36             Long ITEM_ID = someItem.getId();
37 
38             tx.begin();
39             em = JPA.createEntityManager();
40             Item item = em.find(Item.class, ITEM_ID);
41             assertEquals(item.getImages().size(), 4);
42             assertEquals(item.getImages().get(0), "foo.jpg");
43             assertEquals(item.getImages().get(1), "bar.jpg");
44             assertEquals(item.getImages().get(2), "baz.jpg");
45             assertEquals(item.getImages().get(3), "baz.jpg");
46             tx.commit();
47             em.close();
48         } finally {
49             TM.rollback();
50         }
51     }
52 
53 }

 

3.

1 <persistence-unit name="ListOfStringsPU">
2         <jta-data-source>myDS</jta-data-source>
3         <class>org.jpwh.model</class>
4         <class>org.jpwh.model.collections.listofstrings.Item</class>
5         <exclude-unlisted-classes>true</exclude-unlisted-classes>
6     </persistence-unit>

不足之处,无论插入还是删除元素,Hibernate都需要update其他元素的位置

posted @ 2016-04-08 18:08  shamgod  阅读(1441)  评论(0编辑  收藏  举报
haha