码迷,mamicode.com
首页 > 移动开发 > 详细

JavaPersistenceWithHibernate第二版笔记-第七章-001Mapping a set(@ElementCollection、@CollectionTable、@JoinColumn、)

时间:2016-04-08 18:20:31      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

一、结构

技术分享

技术分享

二、代码

1.

 1 package org.jpwh.model.collections.setofstrings;
 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.JoinColumn;
12 import java.util.HashSet;
13 import java.util.Set;
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(
24             name = "IMAGE", // Defaults to ITEM_IMAGES
25             joinColumns = @JoinColumn(name = "ITEM_ID")) // Default, actually
26     @Column(name = "FILENAME") // Defaults to IMAGES
27     protected Set<String> images = new HashSet<String>(); // Initialize field here
28 
29     public Long getId() {
30         return id;
31     }
32 
33     public Set<String> getImages() {
34         return images;
35     }
36 
37     public void setImages(Set<String> images) {
38         this.images = images;
39     }
40 
41     // ...
42 }

 

2.

 1 package org.jpwh.test.collections;
 2 
 3 import org.jpwh.env.JPATest;
 4 import org.jpwh.model.collections.setofstrings.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 SetOfStrings extends JPATest {
14 
15     @Override
16     public void configurePersistenceUnit() throws Exception {
17         configurePersistenceUnit("SetOfStringsPU");
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"); // Duplicate, filtered at Java level by HashSet!
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(), 3);
42             tx.commit();
43             em.close();
44         } finally {
45             TM.rollback();
46         }
47     }
48 
49 }

It doesn’t seem likely that you’d allow the user to attach the same image more than once to the same item,

 

JavaPersistenceWithHibernate第二版笔记-第七章-001Mapping a set(@ElementCollection、@CollectionTable、@JoinColumn、)

标签:

原文地址:http://www.cnblogs.com/shamgod/p/5369082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!