一、结构

二、代码

1.

 package org.jpwh.model.collections.listofstrings;

 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 javax.persistence.OrderColumn;
import java.util.ArrayList;
import java.util.List; @Entity
public class Item { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @ElementCollection
@CollectionTable(name = "IMAGE")
@OrderColumn // Enables persistent order, Defaults to IMAGES_ORDER
@Column(name = "FILENAME")
protected List<String> images = new ArrayList<String>(); public Long getId() {
return id;
} public List<String> getImages() {
return images;
} public void setImages(List<String> images) {
this.images = images;
} // ...
}

2.

 package org.jpwh.test.collections;

 import org.jpwh.env.JPATest;
import org.jpwh.model.collections.listofstrings.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 ListOfStrings extends JPATest { @Override
public void configurePersistenceUnit() throws Exception {
configurePersistenceUnit("ListOfStringsPU");
} @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);
assertEquals(item.getImages().get(0), "foo.jpg");
assertEquals(item.getImages().get(1), "bar.jpg");
assertEquals(item.getImages().get(2), "baz.jpg");
assertEquals(item.getImages().get(3), "baz.jpg");
tx.commit();
em.close();
} finally {
TM.rollback();
}
} }

3.

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

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

JavaPersistenceWithHibernate第二版笔记-第七章-003Mapping an identifier bag(@OrderColumn、@ElementCollection、@CollectionTable、、)的更多相关文章

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

    一.结构 A bag is an unordered collection that allows duplicate elements, like the java.util.Collection ...

  2. JavaPersistenceWithHibernate第二版笔记-第七章-004Mapping a map(@MapKeyEnumerated 、 @MapKeyTemporal、@MapKeyColumn)

    一.结构 二.代码 1. package org.jpwh.model.collections.mapofstrings; import org.jpwh.model.Constants; impor ...

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

    一.结构 二.代码 1. package org.jpwh.model.collections.setofstrings; import org.jpwh.model.Constants; impor ...

  4. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-003Table per concrete class with unions(@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)、<union-subclass>)

    一.代码 1. package org.jpwh.model.inheritance.tableperclass; import org.jpwh.model.Constants; import ja ...

  5. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSup ...

  6. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-001Hibernate映射继承的方法

    There are four different strategies for representing an inheritance hierarchy: Use one table per co ...

  7. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-007UserTypes的用法(@org.hibernate.annotations.Type、@org.hibernate.annotations.TypeDefs、CompositeUserType、DynamicParameterizedType、、、)

    一.结构 二.Hibernate支持的UserTypes接口  UserType —You can transform values by interacting with the plain JD ...

  8. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-006类型转换器( @Converter(autoApply = true) 、type="converter:qualified.ConverterName" )

    一.结构 二.代码 1. package org.jpwh.model.advanced; import java.io.Serializable; import java.math.BigDecim ...

  9. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-005控制类型映射(Nationalized、@LOB、@org.hibernate.annotations.Type)

    一.简介 1. 2. 3. 4. to override this default mapping. The JPA specification has a convenient shortcut a ...

随机推荐

  1. //封装tabs函数

    //封装tabs函数function tabs(tabTit,on,tabCon){ $(tabCon).each(function(){ $(this).children().eq(0).show( ...

  2. Hibernate(1)

    一.什么是hibernate 1. hibernate是开源的轻量级框架,应用在javaee三层结构中 dao层框架,使用orm思想对数据库进行crud操作 2 .在dao层里面做对数据库crud操作 ...

  3. git 远程库 创建私钥

    1.创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步.如果没有,打开Shell(W ...

  4. dirent.h

    #include <dirent.h> 是POSIX.1标准定义的unix类目录操作的头文件,包含了许多UNIX系统服务的函数原型,例如opendir函数.readdir函数. opend ...

  5. MPEG4、XVID、AVC有什么区别

    MPEG-4包含XviD和DivX,而AVC优于二者mpeg4 DVD用的多,101mpeg4有AVC格式(加强版MP4)AVC/H.264是一种最新且技术含量最高的视频编码格式,由MPEG-4标准进 ...

  6. bzoj 5016 一个简单的询问

    THUWC 考了莫队(这个应该可以说吧) 然而不会莫队,签到失败,所以找到了一道长得差不多的题写一写 为什么这么长时间都没有发现这道题(半恼 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问 ...

  7. LeetCode Maximum Length of Pair Chain

    原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n  ...

  8. openfl更新2.0后,android输入法又不能输中文了

    今天手贱更新了一下openfl,fd里面又各种报错.最伤心的是,之前修改MainView.java输入中文的方法现在居然失效了.还好这段时间研究c2dx,总算是能读能懂修改的那段代码,捣鼓了一下午,算 ...

  9. c# OrderBy 实现List升序降序

    本文转载自:http://blog.csdn.net/chadcao/article/details/8730132 1)前台代码 <%@ Page Language="C#" ...

  10. NB-LOT 科普

    最全科普!你一定要了解的NB-IoT 2017-06-19 21:04物联网/操作系统/科普 工信部下发通知推动150万NB-IoT基站落地.NB-IoT汹涌而来.很多网友要求雇佣军科普一篇NB-Io ...