一、结构

二、代码

1.

  1. package org.jpwh.model.collections.mapofstrings;
  2.  
  3. import org.jpwh.model.Constants;
  4.  
  5. import javax.persistence.CollectionTable;
  6. import javax.persistence.Column;
  7. import javax.persistence.ElementCollection;
  8. import javax.persistence.Entity;
  9. import javax.persistence.GeneratedValue;
  10. import javax.persistence.Id;
  11. import javax.persistence.MapKeyColumn;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14.  
  15. @Entity
  16. public class Item {
  17.  
  18. @Id
  19. @GeneratedValue(generator = Constants.ID_GENERATOR)
  20. protected Long id;
  21.  
  22. @ElementCollection
  23. @CollectionTable(name = "IMAGE")
  24. @MapKeyColumn(name = "FILENAME")
  25. @Column(name = "IMAGENAME")
  26. protected Map<String, String> images = new HashMap<String, String>();
  27.  
  28. public Long getId() {
  29. return id;
  30. }
  31.  
  32. public Map<String, String> getImages() {
  33. return images;
  34. }
  35.  
  36. public void setImages(Map<String, String> images) {
  37. this.images = images;
  38. }
  39.  
  40. // ...
  41. }

2.

  1. package org.jpwh.test.collections;
  2.  
  3. import org.jpwh.env.JPATest;
  4. import org.jpwh.model.collections.mapofstrings.Item;
  5. import org.testng.annotations.BeforeClass;
  6. import org.testng.annotations.Test;
  7.  
  8. import javax.persistence.EntityManager;
  9. import javax.transaction.UserTransaction;
  10.  
  11. import static org.testng.Assert.assertEquals;
  12.  
  13. public class MapOfStrings extends JPATest {
  14.  
  15. @Override
  16. public void configurePersistenceUnit() throws Exception {
  17. configurePersistenceUnit("MapOfStringsPU");
  18. }
  19.  
  20. @Test
  21. public void storeLoadCollection() throws Exception {
  22. UserTransaction tx = TM.getUserTransaction();
  23. try {
  24. tx.begin();
  25. EntityManager em = JPA.createEntityManager();
  26. Item someItem = new Item();
  27.  
  28. someItem.getImages().put("foo.jpg", "Foo");
  29. someItem.getImages().put("bar.jpg", "Bar");
  30. someItem.getImages().put("baz.jpg", "WRONG!");
  31. someItem.getImages().put("baz.jpg", "Baz");
  32.  
  33. em.persist(someItem);
  34. tx.commit();
  35. em.close();
  36. Long ITEM_ID = someItem.getId();
  37.  
  38. tx.begin();
  39. em = JPA.createEntityManager();
  40. Item item = em.find(Item.class, ITEM_ID);
  41. assertEquals(item.getImages().size(), 3);
  42. assertEquals(item.getImages().get("foo.jpg"), "Foo");
  43. assertEquals(item.getImages().get("bar.jpg"), "Bar");
  44. assertEquals(item.getImages().get("baz.jpg"), "Baz");
  45. tx.commit();
  46. em.close();
  47. } finally {
  48. TM.rollback();
  49. }
  50. }
  51.  
  52. }

3.

  1. <persistence-unit name="MapOfStringsPU">
  2. <jta-data-source>myDS</jta-data-source>
  3. <class>org.jpwh.model</class>
  4. <class>org.jpwh.model.collections.mapofstrings.Item</class>
  5. <exclude-unlisted-classes>true</exclude-unlisted-classes>
  6. </persistence-unit>

JavaPersistenceWithHibernate第二版笔记-第七章-004Mapping a map(@MapKeyEnumerated 、 @MapKeyTemporal、@MapKeyColumn)的更多相关文章

  1. JavaPersistenceWithHibernate第二版笔记-第七章-003Mapping an identifier bag(@OrderColumn、@ElementCollection、@CollectionTable、、)

    一.结构 二.代码 1. package org.jpwh.model.collections.listofstrings; import org.jpwh.model.Constants; impo ...

  2. JavaPersistenceWithHibernate第二版笔记-第七章-002Mapping an identifier bag(@CollectionId、@ElementCollection、@CollectionTable、@Type)

    一.结构 A bag is an unordered collection that allows duplicate elements, like the java.util.Collection ...

  3. JavaPersistenceWithHibernate第二版笔记-第七章-001Mapping a set(@ElementCollection、@CollectionTable、@JoinColumn、)

    一.结构 二.代码 1. package org.jpwh.model.collections.setofstrings; import org.jpwh.model.Constants; impor ...

  4. 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 ...

  5. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSup ...

  6. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-001Hibernate映射继承的方法

    There are four different strategies for representing an inheritance hierarchy: Use one table per co ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. poj-1426-Find The Multiple(打表水过)

    思路: 2的最近可以整除的数是10 所以,很关键的一点,只要是偶数,例如: 6:2*3,可以拆分为能够被2整除和能够被3整除的乘积,所以,10*111=1110 144:72*2,8*9*2,2*2* ...

  2. (转)关于Linux核心转储文件 core dump

    所谓核心转储文件是内含进程终止时内存映像的一个文件.产生条件:特定的信号会引发进程创建一个核心转储文件并终止运行. 包括哪些特定信号,请参见http://man7.org/linux/man-page ...

  3. MySql 创建函数 Error Code : 1418

    查看日志信息:show variables like 'log_%';显示'log_bin'.'log_bin_trust_function_creators'等状态 解决方法: 关闭binary l ...

  4. 真机环境spotlight光源丢失

    maya做好的模型生成的fbx,导入到unity之后,pc运行正常,到了ios真机上发现光线丢失,场景内物体都是暗暗的,查出来原因是spot光源丢失了,选中spot光源,在其Render Mode里, ...

  5. sed----Linux下文本处理五大神器之一

    转自:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行 ...

  6. C/C++变量命名规则,个人习惯总结【转载】

    C_C++变量命名规则 原文地址:http://blog.sina.com.cn/s/blog_8a7012cf01017h9p.html 变量命名规则是为了增强代码的可读性和容易维护性.以下为C++ ...

  7. jraiser模块加载执行简要总结

    1 在html文件中,通过require方式来加载指定的入口文件:2 然后通过正则表达式来匹配入口文件中的所有require的依赖文件:注意,此时入口文件已加载完毕,不过,还没执行而已.3 之后逐一通 ...

  8. bzoj 2142 礼物——扩展lucas模板

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2142 没给P的范围,但说 pi ^ ci<=1e5,一看就是扩展lucas. 学习材料 ...

  9. selenium+headless chrome安装使用

    pip install selenium 因为phantomJS将停止维护,所以建议使用headless chromeChromeDriver is a separate executable tha ...

  10. STM32中printf重定向到串口

    学习STM32过程中,经常打交道的莫过于串口,你可以将任何信息,当然重要的是调试信息打印到串口中输出,总是用一个字节发送函数或者字符串发送函数 总是有些不放便,之前编程中熟悉的莫过于printf了,下 ...