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 ...
随机推荐
- poj-1426-Find The Multiple(打表水过)
思路: 2的最近可以整除的数是10 所以,很关键的一点,只要是偶数,例如: 6:2*3,可以拆分为能够被2整除和能够被3整除的乘积,所以,10*111=1110 144:72*2,8*9*2,2*2* ...
- (转)关于Linux核心转储文件 core dump
所谓核心转储文件是内含进程终止时内存映像的一个文件.产生条件:特定的信号会引发进程创建一个核心转储文件并终止运行. 包括哪些特定信号,请参见http://man7.org/linux/man-page ...
- MySql 创建函数 Error Code : 1418
查看日志信息:show variables like 'log_%';显示'log_bin'.'log_bin_trust_function_creators'等状态 解决方法: 关闭binary l ...
- 真机环境spotlight光源丢失
maya做好的模型生成的fbx,导入到unity之后,pc运行正常,到了ios真机上发现光线丢失,场景内物体都是暗暗的,查出来原因是spot光源丢失了,选中spot光源,在其Render Mode里, ...
- sed----Linux下文本处理五大神器之一
转自:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行 ...
- C/C++变量命名规则,个人习惯总结【转载】
C_C++变量命名规则 原文地址:http://blog.sina.com.cn/s/blog_8a7012cf01017h9p.html 变量命名规则是为了增强代码的可读性和容易维护性.以下为C++ ...
- jraiser模块加载执行简要总结
1 在html文件中,通过require方式来加载指定的入口文件:2 然后通过正则表达式来匹配入口文件中的所有require的依赖文件:注意,此时入口文件已加载完毕,不过,还没执行而已.3 之后逐一通 ...
- bzoj 2142 礼物——扩展lucas模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2142 没给P的范围,但说 pi ^ ci<=1e5,一看就是扩展lucas. 学习材料 ...
- selenium+headless chrome安装使用
pip install selenium 因为phantomJS将停止维护,所以建议使用headless chromeChromeDriver is a separate executable tha ...
- STM32中printf重定向到串口
学习STM32过程中,经常打交道的莫过于串口,你可以将任何信息,当然重要的是调试信息打印到串口中输出,总是用一个字节发送函数或者字符串发送函数 总是有些不放便,之前编程中熟悉的莫过于printf了,下 ...