/
PersistentMapTest.java
218 lines (183 loc) · 6.15 KB
/
PersistentMapTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.collection.map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToMany;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.collection.internal.PersistentMap;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* Test various situations using a {@link PersistentMap}.
*
* @author Steve Ebersole
* @author Brett Meyer
*/
public class PersistentMapTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "collection/map/Mappings.hbm.xml" };
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { User.class, UserData.class };
}
@Test
@SuppressWarnings({ "unchecked" })
public void testWriteMethodDirtying() {
Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
parent.getChildren().put( child.getName(), child );
child.setParent( parent );
Child otherChild = new Child( "c2" );
Session session = openSession();
session.beginTransaction();
session.save( parent );
session.flush();
// at this point, the map on parent has now been replaced with a PersistentMap...
PersistentMap children = ( PersistentMap ) parent.getChildren();
Object old = children.put( child.getName(), child );
assertTrue( old == child );
assertFalse( children.isDirty() );
old = children.remove( otherChild.getName() );
assertNull( old );
assertFalse( children.isDirty() );
HashMap otherMap = new HashMap();
otherMap.put( child.getName(), child );
children.putAll( otherMap );
assertFalse( children.isDirty() );
otherMap = new HashMap();
otherMap.put( otherChild.getName(), otherChild );
children.putAll( otherMap );
assertTrue( children.isDirty() );
children.clearDirty();
session.delete( child );
children.clear();
assertTrue( children.isDirty() );
session.flush();
children.clear();
assertFalse( children.isDirty() );
session.delete( parent );
session.getTransaction().commit();
session.close();
}
@Test
public void testPutAgainstUninitializedMap() {
// prepare map owner...
Session session = openSession();
session.beginTransaction();
Parent parent = new Parent( "p1" );
session.save( parent );
session.getTransaction().commit();
session.close();
// Now, reload the parent and test adding children
session = openSession();
session.beginTransaction();
parent = ( Parent ) session.get( Parent.class, parent.getName() );
parent.addChild( "c1" );
parent.addChild( "c2" );
session.getTransaction().commit();
session.close();
assertEquals( 2, parent.getChildren().size() );
session = openSession();
session.beginTransaction();
session.delete( parent );
session.getTransaction().commit();
session.close();
}
@Test
public void testRemoveAgainstUninitializedMap() {
Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
parent.addChild( child );
Session session = openSession();
session.beginTransaction();
session.save( parent );
session.getTransaction().commit();
session.close();
// Now reload the parent and test removing the child
session = openSession();
session.beginTransaction();
parent = ( Parent ) session.get( Parent.class, parent.getName() );
Child child2 = ( Child ) parent.getChildren().remove( child.getName() );
child2.setParent( null );
assertNotNull( child2 );
assertTrue( parent.getChildren().isEmpty() );
session.getTransaction().commit();
session.close();
// Load the parent once again and make sure child is still gone
// then cleanup
session = openSession();
session.beginTransaction();
parent = ( Parent ) session.get( Parent.class, parent.getName() );
assertTrue( parent.getChildren().isEmpty() );
session.delete( child2 );
session.delete( parent );
session.getTransaction().commit();
session.close();
}
@Test
@TestForIssue(jiraKey = "HHH-5732")
public void testClearMap() {
Session s = openSession();
s.beginTransaction();
User user = new User();
UserData userData = new UserData();
userData.user = user;
user.userDatas.put( "foo", userData );
s.persist( user );
s.getTransaction().commit();
s.clear();
s.beginTransaction();
user = s.get( User.class, 1 );
user.userDatas.clear();
s.update( user );
Query q = s.createQuery( "DELETE FROM " + UserData.class.getName() + " d WHERE d.user = :user" );
q.setParameter( "user", user );
q.executeUpdate();
s.getTransaction().commit();
s.getTransaction().begin();
assertEquals( s.get( User.class, user.id ).userDatas.size(), 0 );
assertEquals( s.createQuery( "FROM " + UserData.class.getName() ).list().size(), 0 );
s.createQuery( "delete " + User.class.getName() ).executeUpdate();
s.getTransaction().commit();
s.close();
}
@Entity
private static class User implements Serializable {
@Id @GeneratedValue
private Integer id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL)
@MapKeyColumn(name = "name", nullable = true)
private Map<String, UserData> userDatas = new HashMap<String, UserData>();
}
@Entity
private static class UserData {
@Id @GeneratedValue
private Integer id;
@ManyToOne
@JoinColumn(name = "userId")
private User user;
}
}