JavaPersistenceWithHibernate第二版笔记-第七章-004Mapping a map(@MapKeyEnumerated 、 @MapKeyTemporal、@MapKeyColumn)

一、结构

二、代码

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>
时间: 2024-12-30 23:28:06

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

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

一.结构 二.代码 1. 1 package org.jpwh.model.collections.listofstrings; 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.persis

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

一.结构 二.代码 1. 1 package org.jpwh.model.collections.setofstrings; 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.persist

JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-004Table per class hierarchy(@Inheritance..SINGLE_TABLE)、@DiscriminatorColumn、@DiscriminatorValue、@DiscriminatorFormula)

一.结构 You can map an entire class hierarchy to a single table. This table includes columns for all properties of all classes in the hierarchy. The value of an extra type discriminator column or formula identifies the concrete subclass represented by a

JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-001区分entities and value types

一.介绍 1.这种引用方式不对,但删除时不能级联 要这种引用方式 2.The Bid class could be a problem. In object-oriented modeling, this is marked as a composition (the association between Item and Bid with the diamond). Thus, an Item is the owner of its Bid instances and holds a col

JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-004嵌套组件的注解AttributeOverrides

一.数据库 二.代码 1. 1 package org.jpwh.model.advanced; 2 3 import javax.persistence.AttributeOverride; 4 import javax.persistence.AttributeOverrides; 5 import javax.persistence.Column; 6 import javax.persistence.Embeddable; 7 import javax.validation.constr

JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解

一.简介 1.You now have three methods for distinguishing references: ? Objects are identical if they occupy the same memory location in the JVM . This can be checked with the a == b operator. This concept is known as object identity.? Objects are equal i

JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-003映射实体时的可选操作(&lt;delimited-identifiers/&gt;、PhysicalNamingStrategy、PhysicalNamingStrategyStandardImpl、、、)

一.自定义映射的表名 1. 1 @Entity 2 @Table(name = "USERS") 3 public class User implements Serializable { 4 // ... 5 } 2.用定界符 1 //@Table(name = "`USER`")的标准 2 @Table(name = "`USER`") 3 4 //JPA的标准 5 @Table(name = "\"USER\"

读书笔记--《Python基础教程第二版》--第七章 更加抽象

7.1 对象的魔力 多态 不同的类的对象使用同样的操作 封装 继承 7.1.1 多态 1.多态和方法 >>>object.getPrice() >>> 'abc'.count('a') 1 >>> [1,2,'a'].count('a') 1 >>> from random import choice >>> x=choice(['Hello world!',[1,2,'e','e',4]]) >>>

SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-006- 如何保持重定向的request数据(用model、占位符、RedirectAttributes)

一.redirect为什么会丢数据? when a handler method completes, any model data specified in the method is copied into the request as request attributes, and the request is forwarded to the view for rendering. Because it’s the same request that’s handled by both