JavaPersistenceWithHibernate第二版笔记-第七章-002Mapping an identifier bag(@CollectionId、@ElementCollection、@CollectionTable、@Type)
2024-10-21 07:55:34
一、结构
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();
}
} }
最新文章
- selenium 打开浏览器
- DSP, SSP, DMP
- Date Range Picker时间插件非常不错,主要体现在选择一个时间区间
- centos之Haproxy 负载均衡学习笔记
- junit类找不到的问题解决
- hdu 1712 ACboy needs your help 分组背包
- Directory类
- BZOJ 3155: Preprefix sum( 线段树 )
- Oracle常见操作汇总(转)
- Oracle_SQL99_连接查询
- 转载 USB固件分析
- 数据拟合:多项式拟合polynomial curve fitting
- gzy的摄影梦
- UML示例图 zt
- [Oracle,2018-03-02] oracle一次插入多条记录
- h5端呼起摄像头扫描二维码并解析
- usb-cam (2)摄像机标定
- redis简介(未完成)
- LG3978 【[TJOI2015]概率论】
- Mysql缓存中innodb_buffer_pool与Qcache的区别