JavaPersistenceWithHibernate第二版笔记-第七章-004Mapping a map(@MapKeyEnumerated 、 @MapKeyTemporal、@MapKeyColumn)
一、结构
二、代码
1.
package org.jpwh.model.collections.mapofstrings; 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.MapKeyColumn;
import java.util.HashMap;
import java.util.Map; @Entity
public class Item { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @ElementCollection
@CollectionTable(name = "IMAGE")
@MapKeyColumn(name = "FILENAME")
@Column(name = "IMAGENAME")
protected Map<String, String> images = new HashMap<String, String>(); public Long getId() {
return id;
} public Map<String, String> getImages() {
return images;
} public void setImages(Map<String, String> images) {
this.images = images;
} // ...
}
2.
package org.jpwh.test.collections; import org.jpwh.env.JPATest;
import org.jpwh.model.collections.mapofstrings.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 MapOfStrings extends JPATest { @Override
public void configurePersistenceUnit() throws Exception {
configurePersistenceUnit("MapOfStringsPU");
} @Test
public void storeLoadCollection() throws Exception {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
Item someItem = new Item(); someItem.getImages().put("foo.jpg", "Foo");
someItem.getImages().put("bar.jpg", "Bar");
someItem.getImages().put("baz.jpg", "WRONG!");
someItem.getImages().put("baz.jpg", "Baz"); 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);
assertEquals(item.getImages().get("foo.jpg"), "Foo");
assertEquals(item.getImages().get("bar.jpg"), "Bar");
assertEquals(item.getImages().get("baz.jpg"), "Baz");
tx.commit();
em.close();
} finally {
TM.rollback();
}
} }
3.
<persistence-unit name="MapOfStringsPU">
<jta-data-source>myDS</jta-data-source>
<class>org.jpwh.model</class>
<class>org.jpwh.model.collections.mapofstrings.Item</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
JavaPersistenceWithHibernate第二版笔记-第七章-004Mapping a map(@MapKeyEnumerated 、 @MapKeyTemporal、@MapKeyColumn)的更多相关文章
- 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第二版笔记-第七章-001Mapping a set(@ElementCollection、@CollectionTable、@JoinColumn、)
一.结构 二.代码 1. package org.jpwh.model.collections.setofstrings; import org.jpwh.model.Constants; impor ...
- 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 ...
随机推荐
- js 读取json数据 挖坑
一般遇到后台给的json数据格式不对 比如key和value都是单引号. 但真正的json 的key和value都是双引号,必须双引号才能取值. 再来一个例子看看 var test = [{ &quo ...
- hbase_异常_04_util.FSUtils: Waiting for dfs to exit safe mode...
一.异常现象 启动hbase的时,hbase的日志中可以发现: Waiting for dfs to exit safe mode... 然后就抛异常了 2018-03-22 17:00:28,994 ...
- mac 上 mamp 配置虚拟主机 具体过程 ?
https://www.zhihu.com/question/32320396 mac 上 mamp 配置虚拟主机 具体过程 ? 按照哪些配置什么的 都搞好了 但是还不好 配置的主机 制定的目录 还是 ...
- HDU3518Boring counting(后缀自动机)
Problem Description 035 now faced a tough problem,his english teacher gives him a string,which consi ...
- 《Javascript高级程序设计》阅读记录(五):第六章 上
这个系列以往文字地址: <Javascript高级程序设计>阅读记录(一):第二.三章 <Javascript高级程序设计>阅读记录(二):第四章 <Javascript ...
- LeetCode Distribute Candies
原题链接在这里:https://leetcode.com/problems/distribute-candies/#/description 题目: Given an integer array wi ...
- LintCode Sliding Window Matrix Maximum
原题链接在这里:http://www.lintcode.com/zh-cn/problem/sliding-window-matrix-maximum/ 题目: Given an array of n ...
- django的get_or_create
转:http://www.nanerbang.com/article/51/ get_or_create会根据条件从数据库里面查找符合条件的记录,如果没有符合条件的记录,则新创建一条记录
- 软件架构设计 ADMEMS方法体系
ADMEMS是Architecture Design Method has been Extended to Method System的简称,是由CSAI顾问团架构设计专家组于2009年11月在第六 ...
- Nexus安装以及2,3比较
解压缩之后, 进入nexus-3.6.2-01/bin文件夹中,执行: ./nexus start 如果使用root将会得到一个告警:Detected execution as "root& ...