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 ...
随机推荐
- LeetCode OJ:Contains DuplicateII(是否包含重复II)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- Python安装升级步骤
1)安装Pyhton2.7wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2tar xjvf Python-2.7.5.t ...
- php写入数据到mysql数据库中出现乱码解决方法
乱码情况: 在选择数据库前加入一句代码即可 mysql_query("set names utf8"); 最后效果
- 在.net中读写config文件的各种方法(自定义config节点)
http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 阅读目录 开始 config文件 - 自定义配置节点 config文件 - ...
- 笔记:加 ly 不一定是副词
笔记:加 ly 不一定是副词 加 ly 变副词,但有些单词以 ly 结尾,长得像副词,却是形容词. costly = cost + ly a costly item. 一件昂贵的物品. lovely ...
- Operating System-进程间互斥的问题-生产者&&消费者引入
之前介绍的几种解决进程间互斥的方案,不管是Peterson方案还是TSL指令的方式,都有一个特点:当一个进程被Block到临界区外面时,被Block的进程会一直处于忙等待的状态,这个不但浪费了CPU资 ...
- PHP5之前的构造函数与PHP5之后的构造函数的区别
在PHP5以前的版本中,构造函数的名称必须与类名相同,这种方法在PHP5中仍然可以使用,但现在已经很少有人用了. PHP5以及之后的版本,构造函数用__construct()方法来声明,这样做的好处是 ...
- JSF页面中的JS取得受管bean的数据(受管bean发送数据到页面)
JSF中引入jsf.js文件之后,可以拦截jsf.ajax.request请求.一直希望有一种方法可以像jquery的ajax一样,能在js中异步取得服务器端发送的数据.无奈标准JSF并没有提供这样的 ...
- HDU5469(树的dfs)
Antonidas Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- Spark on yarn的两种模式 yarn-cluster 和 yarn-client
从深层次的含义讲,yarn-cluster和yarn-client模式的区别其实就是Application Master进程的区别,yarn-cluster模式下,driver运行在AM(Applic ...