一、结构

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.

 package org.jpwh.model.collections.bagofstrings;

 import org.jpwh.model.Constants;

 import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.ArrayList;
import java.util.Collection; @Entity
public class Item { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @ElementCollection
@CollectionTable(name = "IMAGE")
@Column(name = "FILENAME")
@org.hibernate.annotations.CollectionId( // Surrogate PK allows duplicates!
columns = @Column(name = "IMAGE_ID"),
type = @org.hibernate.annotations.Type(type = "long"),
generator = Constants.ID_GENERATOR)
protected Collection<String> images = new ArrayList<String>(); // No BagImpl in JDK! public Long getId() {
return id;
} public Collection<String> getImages() {
return images;
} public void setImages(Collection<String> images) {
this.images = images;
} // ...
}

2.

 package org.jpwh.test.collections;

 import org.jpwh.env.JPATest;
import org.jpwh.model.collections.bagofstrings.Item;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import javax.persistence.EntityManager;
import javax.transaction.UserTransaction; import static org.testng.Assert.assertEquals; public class BagOfStrings extends JPATest { @Override
public void configurePersistenceUnit() throws Exception {
configurePersistenceUnit("BagOfStringsPU");
} @Test
public void storeLoadCollection() throws Exception {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
Item someItem = new Item(); someItem.getImages().add("foo.jpg");
someItem.getImages().add("bar.jpg");
someItem.getImages().add("baz.jpg");
someItem.getImages().add("baz.jpg"); em.persist(someItem);
tx.commit();
em.close();
Long ITEM_ID = someItem.getId(); tx.begin();
em = JPA.createEntityManager();
Item item = em.find(Item.class, ITEM_ID);
assertEquals(item.getImages().size(), 4);
tx.commit();
em.close();
} finally {
TM.rollback();
}
} }

3.

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

4.

 package org.jpwh.test.collections;

 import org.jpwh.env.JPATest;
import org.jpwh.model.collections.bagofstrings.Item;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import javax.persistence.EntityManager;
import javax.transaction.UserTransaction; import static org.testng.Assert.assertEquals; public class BagOfStrings extends JPATest { @Override
public void configurePersistenceUnit() throws Exception {
configurePersistenceUnit("BagOfStringsPU");
} @Test
public void storeLoadCollection() throws Exception {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
Item someItem = new Item(); someItem.getImages().add("foo.jpg");
someItem.getImages().add("bar.jpg");
someItem.getImages().add("baz.jpg");
someItem.getImages().add("baz.jpg"); em.persist(someItem);
tx.commit();
em.close();
Long ITEM_ID = someItem.getId(); tx.begin();
em = JPA.createEntityManager();
Item item = em.find(Item.class, ITEM_ID);
assertEquals(item.getImages().size(), 4);
tx.commit();
em.close();
} finally {
TM.rollback();
}
} }

最新文章

  1. selenium 打开浏览器
  2. DSP, SSP, DMP
  3. Date Range Picker时间插件非常不错,主要体现在选择一个时间区间
  4. centos之Haproxy 负载均衡学习笔记
  5. junit类找不到的问题解决
  6. hdu 1712 ACboy needs your help 分组背包
  7. Directory类
  8. BZOJ 3155: Preprefix sum( 线段树 )
  9. Oracle常见操作汇总(转)
  10. Oracle_SQL99_连接查询
  11. 转载 USB固件分析
  12. 数据拟合:多项式拟合polynomial curve fitting
  13. gzy的摄影梦
  14. UML示例图 zt
  15. [Oracle,2018-03-02] oracle一次插入多条记录
  16. h5端呼起摄像头扫描二维码并解析
  17. usb-cam (2)摄像机标定
  18. redis简介(未完成)
  19. LG3978 【[TJOI2015]概率论】
  20. Mysql缓存中innodb_buffer_pool与Qcache的区别

热门文章

  1. android不太常用的控件
  2. winform中DataGridView使用DataGridViewCheckBoxColumn实现RadioBox单选功能
  3. canvas基础学习(四)
  4. codeforces C. Pearls in a Row map的应用
  5. Object 的一个问题
  6. 织梦dedecms如何批量替换文章内容和缩略图
  7. HDU4585 Shaolin (STL和treap)
  8. EF各版本增删查改及执行Sql语句
  9. BZOJ4861 [Beijing2017]魔法咒语
  10. 在linux里建立一个快捷方式,连接到另一个目录