1
//$Id: IdTest.java 15025 2008-08-11 09:14:39Z hardy.ferentschik $
2
package org.hibernate.test.annotations.id;
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;
29
* @author Emmanuel Bernard
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();
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);
61
* Ensures that GenericGenerator annotations wrapped inside a
62
* GenericGenerators holder are bound correctly
64
public void testGenericGenerators() throws Exception {
65
Session s = openSession();
66
Transaction tx = s.beginTransaction();
67
Monkey monkey = new Monkey();
70
assertNotNull(monkey.getId());
75
public void testTableGenerator() throws Exception {
76
Session s = openSession();
77
Transaction tx = s.beginTransaction();
81
Computer c = new Computer();
87
assertEquals("table id not generated", new Integer(1), b.getId());
88
assertEquals("generator should not be shared", new Integer(1), d
90
assertEquals("default value should work", new Long(1), c.getId());
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)));
101
public void testSequenceGenerator() throws Exception {
102
Session s = openSession();
103
Transaction tx = s.beginTransaction();
108
assertNotNull(b.getId());
111
tx = s.beginTransaction();
112
s.delete(s.get(Shoe.class, b.getId()));
117
public void testClassLevelGenerator() throws Exception {
118
Session s = openSession();
119
Transaction tx = s.beginTransaction();
120
Store b = new Store();
124
assertNotNull(b.getId());
127
tx = s.beginTransaction();
128
s.delete(s.get(Store.class, b.getId()));
133
public void testMethodLevelGenerator() throws Exception {
134
Session s = openSession();
135
Transaction tx = s.beginTransaction();
136
Department b = new Department();
140
assertNotNull(b.getId());
143
tx = s.beginTransaction();
144
s.delete(s.get(Department.class, b.getId()));
149
public void testDefaultSequence() throws Exception {
153
tx = s.beginTransaction();
158
assertNotNull(h.getId());
161
tx = s.beginTransaction();
162
Home reloadedHome = (Home) s.get(Home.class, h.getId());
163
assertEquals(h.getId(), reloadedHome.getId());
164
s.delete(reloadedHome);
169
public void testParameterizedAuto() throws Exception {
173
tx = s.beginTransaction();
178
assertNotNull(h.getId());
181
tx = s.beginTransaction();
182
Home reloadedHome = (Home) s.get(Home.class, h.getId());
183
assertEquals(h.getId(), reloadedHome.getId());
184
s.delete(reloadedHome);
189
public void testIdInEmbeddableSuperclass() throws Exception {
193
tx = s.beginTransaction();
194
FirTree chrismasTree = new FirTree();
195
s.persist(chrismasTree);
198
tx = s.beginTransaction();
199
chrismasTree = (FirTree) s.get(FirTree.class, chrismasTree.getId());
200
assertNotNull(chrismasTree);
201
s.delete(chrismasTree);
206
public void testIdClass() throws Exception {
210
tx = s.beginTransaction();
211
Footballer fb = new Footballer("David", "Beckam", "Arsenal");
212
GoalKeeper keeper = new GoalKeeper("Fabien", "Bartez", "OM");
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);
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());
233
tx = s.beginTransaction();
234
fb.setClub("Bimbo FC");
238
// reattach by saveOrUpdate
239
tx = s.beginTransaction();
240
fb.setClub("Bimbo FC SA");
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());
256
public void testColumnDefinition() {
257
Column idCol = (Column) getCfg().getClassMapping(Ball.class.getName())
258
.getIdentifierProperty().getValue().getColumnIterator().next();
259
assertEquals("ball_id", idCol.getName());
262
public void testLowAllocationSize() throws Exception {
266
tx = s.beginTransaction();
268
BreakDance[] bds = new BreakDance[size];
269
for (int i = 0; i < size; i++) {
270
bds[i] = new BreakDance();
274
for (int i = 0; i < size; i++) {
275
assertEquals(i + 1, bds[i].id.intValue());
282
* @see org.hibernate.test.annotations.TestCase#getMappings()
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};
293
* @see org.hibernate.test.annotations.TestCase#getAnnotatedPackages()
295
protected String[] getAnnotatedPackages() {
296
return new String[] { "org.hibernate.test.annotations",
297
"org.hibernate.test.annotations.id" };
301
protected String[] getXmlFiles() {
302
return new String[] { "org/hibernate/test/annotations/orm.xml" };