JavaPersistenceWithHibernate第二版笔记-第七章-002Mapping an identifier bag(@CollectionId、@ElementCollection、@CollectionTable、@Type)

一、结构

A bag is an unordered collection that allows duplicate elements, like the java.util.Collection interface. Curiously, the Java Collections framework doesn’t include a bag implementation. You initialize the property with an ArrayList , and
Hibernate ignores the index of elements when storing and loading elements.

二、代码

1.

 1 package org.jpwh.model.collections.bagofstrings;
 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 java.util.ArrayList;
12 import java.util.Collection;
13 
14 @Entity
15 public class Item {
16 
17     @Id
18     @GeneratedValue(generator = Constants.ID_GENERATOR)
19     protected Long id;
20 
21     @ElementCollection
22     @CollectionTable(name = "IMAGE")
23     @Column(name = "FILENAME")
24     @org.hibernate.annotations.CollectionId( // Surrogate PK allows duplicates!
25             columns = @Column(name = "IMAGE_ID"),
26             type = @org.hibernate.annotations.Type(type = "long"),
27             generator = Constants.ID_GENERATOR)
28     protected Collection<String> images = new ArrayList<String>(); // No BagImpl in JDK!
29 
30     public Long getId() {
31         return id;
32     }
33 
34     public Collection<String> getImages() {
35         return images;
36     }
37 
38     public void setImages(Collection<String> images) {
39         this.images = images;
40     }
41 
42     // ...
43 }

 

2.

 1 package org.jpwh.test.collections;
 2 
 3 import org.jpwh.env.JPATest;
 4 import org.jpwh.model.collections.bagofstrings.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 BagOfStrings extends JPATest {
14 
15     @Override
16     public void configurePersistenceUnit() throws Exception {
17         configurePersistenceUnit("BagOfStringsPU");
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             tx.commit();
43             em.close();
44         } finally {
45             TM.rollback();
46         }
47     }
48 
49 }

 

3.

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

 

4.

 1 package org.jpwh.test.collections;
 2 
 3 import org.jpwh.env.JPATest;
 4 import org.jpwh.model.collections.bagofstrings.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 BagOfStrings extends JPATest {
14 
15     @Override
16     public void configurePersistenceUnit() throws Exception {
17         configurePersistenceUnit("BagOfStringsPU");
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             tx.commit();
43             em.close();
44         } finally {
45             TM.rollback();
46         }
47     }
48 
49 }

 

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