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 ...
随机推荐
- 使用smart-npm和npm安装完毕之后发现 不是内部命令和外部命令!
我明明记得加入过path环境变量.我想一定是什么原因 一查bing.com然后发现没有在全局环境 .所以 只需要过npm install -g gulp 一句执行cmd这样就好 $ gulp提示gu ...
- c++primer 第二章编程练习答案
2.7.1 #include<iostream> int main() { using namespace std; ]; ]; cout << "input nam ...
- 机器学习:YOLO for Object Detection (二)
之前介绍了 YOLO-v1 单纯的利用一个卷积网络完成了目标检测,不过 YOLO-v1 虽然速度很快,但是比起其他的网络比如 Fast R-CNN 检测的准确率还是差不少,所以作者又提出了改良版的 Y ...
- ThinkPHP中的find和select的区别
ThinkPHP作为PHP中应用广泛又好用的框架,能比较快速的开发MVC架构的管理系统,获得了大量的应用.但是在ThinkPHP中select()和find()方法有什么区别呢? 事实上find()返 ...
- HihoCoder1337 动态第k大(treap)
描述 小Ho:小Hi,之前你不是讲过Splay和Treap么,那么还有没有更简单的平衡树呢? 小Hi:但是Splay和Treap不是已经很简单了么? 小Ho:是这样没错啦,但是Splay和Treap和 ...
- BZOJ5341: [Ctsc2018]暴力写挂
BZOJ5341: [Ctsc2018]暴力写挂 https://lydsy.com/JudgeOnline/problem.php?id=5341 分析: 学习边分治. 感觉边分治在多数情况下都能用 ...
- COGS1516. 棋盘上的车
[题目描述] 在n*n(n≤20)的方格棋盘上放置n 个车,求使它们不能互相攻击的方案总数. [输入格式] 一行一个正整数n. [输出格式] 一行一个正整数,即方案总数. [样例输入] 3 [样例输出 ...
- java 连接oracle数据库
package shujuku; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepared ...
- C#中将dateTimePicker初始值设置为空
最近在做一个小项目,有一个功能是根据用户选择条件查询数据,要求时间控件的默认值为空,只有当用户修改了时间,才根据时间查询.简单的说,就是默认或者点击清空按钮的情况下,时间控件dateTimePicke ...
- 学习动态性能表(15)--v$rollstat
学习动态性能表 第15篇--V$ROLLSTAT 2007.6.12 本视图自启动即保持并记录各回滚段统计项.在学习本视图之前,我们先来了解一下回滚段(rollback segment)的相关概念: ...