一、结构

二、代码

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. SpringBoot_06_使用Swagger2构建强大的RESTful API文档

    二.参考资料 1.Spring Boot中使用Swagger2构建强大的RESTful API文档 2.

  2. XMLSchema验证

    一.什么是Schema(XSD) XML Schema是微软定义的一套用来验证XML技术.是一套预先规定的XML元素和属性创建的,这些元素和属性定义了XML文档的结构和内容模式. DTD的局限性: 1 ...

  3. nyoj-155-求高精度幂(java大数)

    题目链接 import java.util.*; import java.math.*; public class Main{ public static void main(String[] arg ...

  4. CodeForces - 891C: Envy(可撤销的并查集&最小生成树)

    For a connected undirected weighted graph G, MST (minimum spanning tree) is a subgraph of G that con ...

  5. LeetCode 369. Plus One Linked List

    原题链接在这里:https://leetcode.com/problems/plus-one-linked-list/ 题目: Given a non-negative number represen ...

  6. LG2865 [USACO06NOV]路障Roadblocks

    题意 Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. ...

  7. 使用python实现两个文件夹里文件的对比(包含内容的对比)

    #-*-coding:utf-8-*- #=============================================================================== ...

  8. 增加 [确定] and [失败]系统提示

    增加 [确定] and  [失败]系统提示 #!/bin/bash. /etc/init.d/functionsaction "true" /bin/falseaction &qu ...

  9. springboot+springcloud config

    参考:sorry,全找不到了,当时没记录,最后后知后觉觉得应该记录,所以后面的都有在asfood父项目中的doc文件夹下记录,望见谅. 1. springconfig server 1.1. pom. ...

  10. mysql函数之三:find_in_set() 函数 返回str在字符串集strlist中的序号

    如果字符串str是在的strlist组成的N子串的字符串列表,返回值的范围为1到N. FIND_IN_SET(str,strlist) 如果字符串str是在的strlist组成的N子串的字符串列表,返 ...