~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-annotations-3.4.0.GA/test/org/hibernate/test/annotations/id/IdTest.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//$Id: IdTest.java 15025 2008-08-11 09:14:39Z hardy.ferentschik $
2
 
package org.hibernate.test.annotations.id;
3
 
 
4
 
import org.hibernate.Session;
5
 
import org.hibernate.Transaction;
6
 
import org.hibernate.dialect.HSQLDialect;
7
 
import org.hibernate.mapping.Column;
8
 
import org.hibernate.test.annotations.RequiresDialect;
9
 
import org.hibernate.test.annotations.TestCase;
10
 
import org.hibernate.test.annotations.id.entities.Ball;
11
 
import org.hibernate.test.annotations.id.entities.BreakDance;
12
 
import org.hibernate.test.annotations.id.entities.Computer;
13
 
import org.hibernate.test.annotations.id.entities.Department;
14
 
import org.hibernate.test.annotations.id.entities.Dog;
15
 
import org.hibernate.test.annotations.id.entities.FirTree;
16
 
import org.hibernate.test.annotations.id.entities.Footballer;
17
 
import org.hibernate.test.annotations.id.entities.FootballerPk;
18
 
import org.hibernate.test.annotations.id.entities.Furniture;
19
 
import org.hibernate.test.annotations.id.entities.GoalKeeper;
20
 
import org.hibernate.test.annotations.id.entities.Home;
21
 
import org.hibernate.test.annotations.id.entities.Monkey;
22
 
import org.hibernate.test.annotations.id.entities.Phone;
23
 
import org.hibernate.test.annotations.id.entities.Shoe;
24
 
import org.hibernate.test.annotations.id.entities.SoundSystem;
25
 
import org.hibernate.test.annotations.id.entities.Store;
26
 
import org.hibernate.test.annotations.id.entities.Tree;
27
 
 
28
 
/**
29
 
 * @author Emmanuel Bernard
30
 
 */
31
 
@SuppressWarnings("unchecked")
32
 
@RequiresDialect(HSQLDialect.class)
33
 
public class IdTest extends TestCase {
34
 
        // FIXME split Sequence and Id tests to explicit the run failure on Oracle etc
35
 
        public void testGenericGenerator() throws Exception {
36
 
                Session s = openSession();
37
 
                Transaction tx = s.beginTransaction();
38
 
                SoundSystem system = new SoundSystem();
39
 
                system.setBrand("Genelec");
40
 
                system.setModel("T234");
41
 
                Furniture fur = new Furniture();
42
 
                s.persist(system);
43
 
                s.persist(fur);
44
 
                tx.commit();
45
 
                s.close();
46
 
 
47
 
                s = openSession();
48
 
                tx = s.beginTransaction();
49
 
                system = (SoundSystem) s.get(SoundSystem.class, system.getId());
50
 
                fur = (Furniture) s.get(Furniture.class, fur.getId());
51
 
                assertNotNull(system);
52
 
                assertNotNull(fur);
53
 
                s.delete(system);
54
 
                s.delete(fur);
55
 
                tx.commit();
56
 
                s.close();
57
 
 
58
 
        }
59
 
 
60
 
        /*
61
 
         * Ensures that GenericGenerator annotations wrapped inside a
62
 
         * GenericGenerators holder are bound correctly
63
 
         */
64
 
        public void testGenericGenerators() throws Exception {
65
 
                Session s = openSession();
66
 
                Transaction tx = s.beginTransaction();
67
 
                Monkey monkey = new Monkey();
68
 
                s.persist(monkey);
69
 
                s.flush();
70
 
                assertNotNull(monkey.getId());
71
 
                tx.rollback();
72
 
                s.close();
73
 
        }
74
 
 
75
 
        public void testTableGenerator() throws Exception {
76
 
                Session s = openSession();
77
 
                Transaction tx = s.beginTransaction();
78
 
 
79
 
                Ball b = new Ball();
80
 
                Dog d = new Dog();
81
 
                Computer c = new Computer();
82
 
                s.persist(b);
83
 
                s.persist(d);
84
 
                s.persist(c);
85
 
                tx.commit();
86
 
                s.close();
87
 
                assertEquals("table id not generated", new Integer(1), b.getId());
88
 
                assertEquals("generator should not be shared", new Integer(1), d
89
 
                                .getId());
90
 
                assertEquals("default value should work", new Long(1), c.getId());
91
 
 
92
 
                s = openSession();
93
 
                tx = s.beginTransaction();
94
 
                s.delete(s.get(Ball.class, new Integer(1)));
95
 
                s.delete(s.get(Dog.class, new Integer(1)));
96
 
                s.delete(s.get(Computer.class, new Long(1)));
97
 
                tx.commit();
98
 
                s.close();
99
 
        }
100
 
 
101
 
        public void testSequenceGenerator() throws Exception {
102
 
                Session s = openSession();
103
 
                Transaction tx = s.beginTransaction();
104
 
                Shoe b = new Shoe();
105
 
                s.persist(b);
106
 
                tx.commit();
107
 
                s.close();
108
 
                assertNotNull(b.getId());
109
 
 
110
 
                s = openSession();
111
 
                tx = s.beginTransaction();
112
 
                s.delete(s.get(Shoe.class, b.getId()));
113
 
                tx.commit();
114
 
                s.close();
115
 
        }
116
 
 
117
 
        public void testClassLevelGenerator() throws Exception {
118
 
                Session s = openSession();
119
 
                Transaction tx = s.beginTransaction();
120
 
                Store b = new Store();
121
 
                s.persist(b);
122
 
                tx.commit();
123
 
                s.close();
124
 
                assertNotNull(b.getId());
125
 
 
126
 
                s = openSession();
127
 
                tx = s.beginTransaction();
128
 
                s.delete(s.get(Store.class, b.getId()));
129
 
                tx.commit();
130
 
                s.close();
131
 
        }
132
 
 
133
 
        public void testMethodLevelGenerator() throws Exception {
134
 
                Session s = openSession();
135
 
                Transaction tx = s.beginTransaction();
136
 
                Department b = new Department();
137
 
                s.persist(b);
138
 
                tx.commit();
139
 
                s.close();
140
 
                assertNotNull(b.getId());
141
 
 
142
 
                s = openSession();
143
 
                tx = s.beginTransaction();
144
 
                s.delete(s.get(Department.class, b.getId()));
145
 
                tx.commit();
146
 
                s.close();
147
 
        }
148
 
 
149
 
        public void testDefaultSequence() throws Exception {
150
 
                Session s;
151
 
                Transaction tx;
152
 
                s = openSession();
153
 
                tx = s.beginTransaction();
154
 
                Home h = new Home();
155
 
                s.persist(h);
156
 
                tx.commit();
157
 
                s.close();
158
 
                assertNotNull(h.getId());
159
 
 
160
 
                s = openSession();
161
 
                tx = s.beginTransaction();
162
 
                Home reloadedHome = (Home) s.get(Home.class, h.getId());
163
 
                assertEquals(h.getId(), reloadedHome.getId());
164
 
                s.delete(reloadedHome);
165
 
                tx.commit();
166
 
                s.close();
167
 
        }
168
 
 
169
 
        public void testParameterizedAuto() throws Exception {
170
 
                Session s;
171
 
                Transaction tx;
172
 
                s = openSession();
173
 
                tx = s.beginTransaction();
174
 
                Home h = new Home();
175
 
                s.persist(h);
176
 
                tx.commit();
177
 
                s.close();
178
 
                assertNotNull(h.getId());
179
 
 
180
 
                s = openSession();
181
 
                tx = s.beginTransaction();
182
 
                Home reloadedHome = (Home) s.get(Home.class, h.getId());
183
 
                assertEquals(h.getId(), reloadedHome.getId());
184
 
                s.delete(reloadedHome);
185
 
                tx.commit();
186
 
                s.close();
187
 
        }
188
 
 
189
 
        public void testIdInEmbeddableSuperclass() throws Exception {
190
 
                Session s;
191
 
                Transaction tx;
192
 
                s = openSession();
193
 
                tx = s.beginTransaction();
194
 
                FirTree chrismasTree = new FirTree();
195
 
                s.persist(chrismasTree);
196
 
                tx.commit();
197
 
                s.clear();
198
 
                tx = s.beginTransaction();
199
 
                chrismasTree = (FirTree) s.get(FirTree.class, chrismasTree.getId());
200
 
                assertNotNull(chrismasTree);
201
 
                s.delete(chrismasTree);
202
 
                tx.commit();
203
 
                s.close();
204
 
        }
205
 
 
206
 
        public void testIdClass() throws Exception {
207
 
                Session s;
208
 
                Transaction tx;
209
 
                s = openSession();
210
 
                tx = s.beginTransaction();
211
 
                Footballer fb = new Footballer("David", "Beckam", "Arsenal");
212
 
                GoalKeeper keeper = new GoalKeeper("Fabien", "Bartez", "OM");
213
 
                s.persist(fb);
214
 
                s.persist(keeper);
215
 
                tx.commit();
216
 
                s.clear();
217
 
 
218
 
                // lookup by id
219
 
                tx = s.beginTransaction();
220
 
                FootballerPk fpk = new FootballerPk("David", "Beckam");
221
 
                fb = (Footballer) s.get(Footballer.class, fpk);
222
 
                FootballerPk fpk2 = new FootballerPk("Fabien", "Bartez");
223
 
                keeper = (GoalKeeper) s.get(GoalKeeper.class, fpk2);
224
 
                assertNotNull(fb);
225
 
                assertNotNull(keeper);
226
 
                assertEquals("Beckam", fb.getLastname());
227
 
                assertEquals("Arsenal", fb.getClub());
228
 
                assertEquals(1, s.createQuery(
229
 
                                "from Footballer f where f.firstname = 'David'").list().size());
230
 
                tx.commit();
231
 
 
232
 
                // reattach by merge
233
 
                tx = s.beginTransaction();
234
 
                fb.setClub("Bimbo FC");
235
 
                s.merge(fb);
236
 
                tx.commit();
237
 
 
238
 
                // reattach by saveOrUpdate
239
 
                tx = s.beginTransaction();
240
 
                fb.setClub("Bimbo FC SA");
241
 
                s.saveOrUpdate(fb);
242
 
                tx.commit();
243
 
 
244
 
                // clean up
245
 
                s.clear();
246
 
                tx = s.beginTransaction();
247
 
                fpk = new FootballerPk("David", "Beckam");
248
 
                fb = (Footballer) s.get(Footballer.class, fpk);
249
 
                assertEquals("Bimbo FC SA", fb.getClub());
250
 
                s.delete(fb);
251
 
                s.delete(keeper);
252
 
                tx.commit();
253
 
                s.close();
254
 
        }
255
 
 
256
 
        public void testColumnDefinition() {
257
 
                Column idCol = (Column) getCfg().getClassMapping(Ball.class.getName())
258
 
                                .getIdentifierProperty().getValue().getColumnIterator().next();
259
 
                assertEquals("ball_id", idCol.getName());
260
 
        }
261
 
 
262
 
        public void testLowAllocationSize() throws Exception {
263
 
                Session s;
264
 
                Transaction tx;
265
 
                s = openSession();
266
 
                tx = s.beginTransaction();
267
 
                int size = 4;
268
 
                BreakDance[] bds = new BreakDance[size];
269
 
                for (int i = 0; i < size; i++) {
270
 
                        bds[i] = new BreakDance();
271
 
                        s.persist(bds[i]);
272
 
                }
273
 
                s.flush();
274
 
                for (int i = 0; i < size; i++) {
275
 
                        assertEquals(i + 1, bds[i].id.intValue());
276
 
                }
277
 
                tx.rollback();
278
 
                s.close();
279
 
        }
280
 
 
281
 
        /**
282
 
         * @see org.hibernate.test.annotations.TestCase#getMappings()
283
 
         */
284
 
        protected Class[] getMappings() {
285
 
                return new Class[] { Ball.class, Shoe.class, Store.class,
286
 
                                Department.class, Dog.class, Computer.class, Home.class,
287
 
                                Phone.class, Tree.class, FirTree.class, Footballer.class,
288
 
                                SoundSystem.class, Furniture.class, GoalKeeper.class,
289
 
                                BreakDance.class, Monkey.class};
290
 
        }
291
 
 
292
 
        /**
293
 
         * @see org.hibernate.test.annotations.TestCase#getAnnotatedPackages()
294
 
         */
295
 
        protected String[] getAnnotatedPackages() {
296
 
                return new String[] { "org.hibernate.test.annotations",
297
 
                                "org.hibernate.test.annotations.id" };
298
 
        }
299
 
 
300
 
        @Override
301
 
        protected String[] getXmlFiles() {
302
 
                return new String[] { "org/hibernate/test/annotations/orm.xml" };
303
 
        }
304
 
 
305
 
}