JavaPersistenceWithHibernate第二版笔记-第七章-001Mapping a set(@ElementCollection、@CollectionTable、@JoinColumn、)
一、结构
二、代码
1.
package org.jpwh.model.collections.setofstrings; 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.JoinColumn;
import java.util.HashSet;
import java.util.Set; @Entity
public class Item { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @ElementCollection
@CollectionTable(
name = "IMAGE", // Defaults to ITEM_IMAGES
joinColumns = @JoinColumn(name = "ITEM_ID")) // Default, actually
@Column(name = "FILENAME") // Defaults to IMAGES
protected Set<String> images = new HashSet<String>(); // Initialize field here public Long getId() {
return id;
} public Set<String> getImages() {
return images;
} public void setImages(Set<String> images) {
this.images = images;
} // ...
}
2.
package org.jpwh.test.collections; import org.jpwh.env.JPATest;
import org.jpwh.model.collections.setofstrings.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 SetOfStrings extends JPATest { @Override
public void configurePersistenceUnit() throws Exception {
configurePersistenceUnit("SetOfStringsPU");
} @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"); // Duplicate, filtered at Java level by HashSet! 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(), 3);
tx.commit();
em.close();
} finally {
TM.rollback();
}
} }
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、)的更多相关文章
- JavaPersistenceWithHibernate第二版笔记-第七章-004Mapping a map(@MapKeyEnumerated 、 @MapKeyTemporal、@MapKeyColumn)
一.结构 二.代码 1. package org.jpwh.model.collections.mapofstrings; import org.jpwh.model.Constants; impor ...
- JavaPersistenceWithHibernate第二版笔记-第七章-003Mapping an identifier bag(@OrderColumn、@ElementCollection、@CollectionTable、、)
一.结构 二.代码 1. package org.jpwh.model.collections.listofstrings; import org.jpwh.model.Constants; impo ...
- JavaPersistenceWithHibernate第二版笔记-第七章-002Mapping an identifier bag(@CollectionId、@ElementCollection、@CollectionTable、@Type)
一.结构 A bag is an unordered collection that allows duplicate elements, like the java.util.Collection ...
- 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 ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)
一.结构 二.代码 1. package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSup ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-001Hibernate映射继承的方法
There are four different strategies for representing an inheritance hierarchy: Use one table per co ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- uva11609(组合数学,快速幂)
先选人,再从这些人里选一个队长,方案总数:C(i,1)*C(n,i)(其中i从1到n)的总和. 这个公式显然不能在时限内暴力算出来,需要变形和推导出更简单的来. 用到组合数里面这个公式:C(n,k)* ...
- CodeForces - 438D: The Child and Sequence(势能线段树)
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...
- django1.7 HTML模板中{%url%}的使用
转载:https://my.oschina.net/jastme/blog/345265
- c#生成唯一编号方法记录,可用数据库主键 唯一+有序
数据库主键目前主要有两种: a.自增数值型 优:占用空间小,插入快,有序对索引友好,易懂 缺:多数据库迁移会有重复键值问题,有可能爆表 b.GUID 优:多数据库唯一 缺:占用空间大,无序对索引不友好 ...
- 软件架构设计 ADMEMS方法体系
ADMEMS是Architecture Design Method has been Extended to Method System的简称,是由CSAI顾问团架构设计专家组于2009年11月在第六 ...
- spring mybatis 多个数据源配置
mybatis生成器:http://blog.csdn.net/tolcf/article/details/50835165 通过命令生成:java -jar mybatis-generator-co ...
- Azure VM的加速网络
Azure的VM在经过几代发展后已经有多种硬件类型.目前Azure China的多种机型都配置了FPGA卡,可以实现网络加速.本文将介绍Azure的加速网络相关的内容. 一. 加速网络的硬件准备 下图 ...
- Xcode工具特性
1.注释 #pragma mark 注释说明#pragma mark - 分类/分组注释说明 2.自定义代码块. 3.多文本编辑框 View>>Assistant Editor
- boot asio 非阻塞同步编程---非阻塞的accept和receive。
boot asio 非阻塞同步编程---非阻塞的accept和receive. 客户端编程: #include<boost/timer.hpp> #include <iostream ...
- SpringMVC---依赖注入与面向切面
1.依赖注入与面向切面 1.1.出现背景 ——如何简化java开发? 其中很重要的一点是“组件化”. ——如何更好的“组件化”? 松耦合,以及尽可能的让组件专注于本身. ——Spring框架的目的也只 ...