-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
EmbeddedTest.java
543 lines (501 loc) · 17.7 KB
/
EmbeddedTest.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.annotations.embedded;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.test.annotations.embedded.FloatLeg.RateIndex;
import org.hibernate.test.annotations.embedded.Leg.Frequency;
import org.hibernate.test.util.SchemaUtil;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Emmanuel Bernard
*/
@FailureExpectedWithNewMetamodel
public class EmbeddedTest extends BaseCoreFunctionalTestCase {
@Test
public void testSimple() throws Exception {
Session s;
Transaction tx;
Person p = new Person();
Address a = new Address();
Country c = new Country();
Country bornCountry = new Country();
c.setIso2( "DM" );
c.setName( "Matt Damon Land" );
bornCountry.setIso2( "US" );
bornCountry.setName( "United States of America" );
a.address1 = "colorado street";
a.city = "Springfield";
a.country = c;
p.address = a;
p.bornIn = bornCountry;
p.name = "Homer";
s = openSession();
tx = s.beginTransaction();
s.persist( p );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
p = (Person) s.get( Person.class, p.id );
assertNotNull( p );
assertNotNull( p.address );
assertEquals( "Springfield", p.address.city );
assertNotNull( p.address.country );
assertEquals( "DM", p.address.country.getIso2() );
assertNotNull( p.bornIn );
assertEquals( "US", p.bornIn.getIso2() );
tx.commit();
s.close();
}
@Test
public void testCompositeId() throws Exception {
Session s;
Transaction tx;
RegionalArticlePk pk = new RegionalArticlePk();
pk.iso2 = "FR";
pk.localUniqueKey = "1234567890123";
RegionalArticle reg = new RegionalArticle();
reg.setName( "Je ne veux pes rester sage - Dolly" );
reg.setPk( pk );
s = openSession();
tx = s.beginTransaction();
s.persist( reg );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
reg = (RegionalArticle) s.get( RegionalArticle.class, reg.getPk() );
assertNotNull( reg );
assertNotNull( reg.getPk() );
assertEquals( "Je ne veux pes rester sage - Dolly", reg.getName() );
assertEquals( "FR", reg.getPk().iso2 );
tx.commit();
s.close();
}
@Test
public void testManyToOneInsideComponent() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Person p = new Person();
Country bornIn = new Country();
bornIn.setIso2( "FR" );
bornIn.setName( "France" );
p.bornIn = bornIn;
p.name = "Emmanuel";
AddressType type = new AddressType();
type.setName( "Primary Home" );
s.persist( type );
Country currentCountry = new Country();
currentCountry.setIso2( "US" );
currentCountry.setName( "USA" );
Address add = new Address();
add.address1 = "4 square street";
add.city = "San diego";
add.country = currentCountry;
add.type = type;
p.address = add;
s.persist( p );
tx.commit();
s = openSession();
tx = s.beginTransaction();
Query q = s.createQuery( "select p from Person p where p.address.city = :city" );
q.setString( "city", add.city );
List result = q.list();
Person samePerson = (Person) result.get( 0 );
assertNotNull( samePerson.address.type );
assertEquals( type.getName(), samePerson.address.type.getName() );
tx.commit();
s.close();
}
@Test
public void testEmbeddedSuperclass() {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
VanillaSwap swap = new VanillaSwap();
swap.setInstrumentId( "US345421" );
swap.setCurrency( VanillaSwap.Currency.EUR );
FixedLeg fixed = new FixedLeg();
fixed.setPaymentFrequency( Leg.Frequency.SEMIANNUALLY );
fixed.setRate( 5.6 );
FloatLeg floating = new FloatLeg();
floating.setPaymentFrequency( Leg.Frequency.QUARTERLY );
floating.setRateIndex( FloatLeg.RateIndex.LIBOR );
floating.setRateSpread( 1.1 );
swap.setFixedLeg( fixed );
swap.setFloatLeg( floating );
s.persist( swap );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
swap = (VanillaSwap) s.get( VanillaSwap.class, swap.getInstrumentId() );
// All fields must be filled with non-default values
fixed = swap.getFixedLeg();
assertNotNull( "Fixed leg retrieved as null", fixed );
floating = swap.getFloatLeg();
assertNotNull( "Floating leg retrieved as null", floating );
assertEquals( Leg.Frequency.SEMIANNUALLY, fixed.getPaymentFrequency() );
assertEquals( Leg.Frequency.QUARTERLY, floating.getPaymentFrequency() );
s.delete( swap );
tx.commit();
s.close();
}
@Test
public void testDottedProperty() {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
// Create short swap
Swap shortSwap = new Swap();
shortSwap.setTenor( 2 );
FixedLeg shortFixed = new FixedLeg();
shortFixed.setPaymentFrequency( Frequency.SEMIANNUALLY );
shortFixed.setRate( 5.6 );
FloatLeg shortFloating = new FloatLeg();
shortFloating.setPaymentFrequency( Frequency.QUARTERLY );
shortFloating.setRateIndex( RateIndex.LIBOR );
shortFloating.setRateSpread( 1.1 );
shortSwap.setFixedLeg( shortFixed );
shortSwap.setFloatLeg( shortFloating );
// Create medium swap
Swap swap = new Swap();
swap.setTenor( 7 );
FixedLeg fixed = new FixedLeg();
fixed.setPaymentFrequency( Frequency.MONTHLY );
fixed.setRate( 7.6 );
FloatLeg floating = new FloatLeg();
floating.setPaymentFrequency( Frequency.MONTHLY );
floating.setRateIndex( RateIndex.TIBOR );
floating.setRateSpread( 0.8 );
swap.setFixedLeg( fixed );
swap.setFloatLeg( floating );
// Create long swap
Swap longSwap = new Swap();
longSwap.setTenor( 7 );
FixedLeg longFixed = new FixedLeg();
longFixed.setPaymentFrequency( Frequency.MONTHLY );
longFixed.setRate( 7.6 );
FloatLeg longFloating = new FloatLeg();
longFloating.setPaymentFrequency( Frequency.MONTHLY );
longFloating.setRateIndex( RateIndex.TIBOR );
longFloating.setRateSpread( 0.8 );
longSwap.setFixedLeg( longFixed );
longSwap.setFloatLeg( longFloating );
// Compose a curve spread deal
SpreadDeal deal = new SpreadDeal();
deal.setId( "FX45632" );
deal.setNotional( 450000.0 );
deal.setShortSwap( shortSwap );
deal.setSwap( swap );
deal.setLongSwap( longSwap );
s.persist( deal );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
deal = (SpreadDeal) s.get( SpreadDeal.class, deal.getId() );
// All fields must be filled with non-default values
assertNotNull( "Short swap is null.", deal.getShortSwap() );
assertNotNull( "Swap is null.", deal.getSwap() );
assertNotNull( "Long swap is null.", deal.getLongSwap() );
assertEquals( 2, deal.getShortSwap().getTenor() );
assertEquals( 7, deal.getSwap().getTenor() );
assertEquals( 7, deal.getLongSwap().getTenor() );
assertNotNull( "Short fixed leg is null.", deal.getShortSwap().getFixedLeg() );
assertNotNull( "Short floating leg is null.", deal.getShortSwap().getFloatLeg() );
assertNotNull( "Fixed leg is null.", deal.getSwap().getFixedLeg() );
assertNotNull( "Floating leg is null.", deal.getSwap().getFloatLeg() );
assertNotNull( "Long fixed leg is null.", deal.getLongSwap().getFixedLeg() );
assertNotNull( "Long floating leg is null.", deal.getLongSwap().getFloatLeg() );
assertEquals( Frequency.SEMIANNUALLY, deal.getShortSwap().getFixedLeg().getPaymentFrequency() );
assertEquals( Frequency.QUARTERLY, deal.getShortSwap().getFloatLeg().getPaymentFrequency() );
assertEquals( Frequency.MONTHLY, deal.getSwap().getFixedLeg().getPaymentFrequency() );
assertEquals( Frequency.MONTHLY, deal.getSwap().getFloatLeg().getPaymentFrequency() );
assertEquals( Frequency.MONTHLY, deal.getLongSwap().getFixedLeg().getPaymentFrequency() );
assertEquals( Frequency.MONTHLY, deal.getLongSwap().getFloatLeg().getPaymentFrequency() );
assertEquals( 5.6, deal.getShortSwap().getFixedLeg().getRate(), 0.01 );
assertEquals( 7.6, deal.getSwap().getFixedLeg().getRate(), 0.01 );
assertEquals( 7.6, deal.getLongSwap().getFixedLeg().getRate(), 0.01 );
assertEquals( RateIndex.LIBOR, deal.getShortSwap().getFloatLeg().getRateIndex() );
assertEquals( RateIndex.TIBOR, deal.getSwap().getFloatLeg().getRateIndex() );
assertEquals( RateIndex.TIBOR, deal.getLongSwap().getFloatLeg().getRateIndex() );
assertEquals( 1.1, deal.getShortSwap().getFloatLeg().getRateSpread(), 0.01 );
assertEquals( 0.8, deal.getSwap().getFloatLeg().getRateSpread(), 0.01 );
assertEquals( 0.8, deal.getLongSwap().getFloatLeg().getRateSpread(), 0.01 );
s.delete( deal );
tx.commit();
s.close();
}
@Test
public void testEmbeddedInSecondaryTable() throws Exception {
Session s;
s = openSession();
s.getTransaction().begin();
Book book = new Book();
book.setIsbn( "1234" );
book.setName( "HiA Second Edition" );
Summary summary = new Summary();
summary.setText( "This is a HiA SE summary" );
summary.setSize( summary.getText().length() );
book.setSummary( summary );
s.persist( book );
s.getTransaction().commit();
s.clear();
Transaction tx = s.beginTransaction();
Book loadedBook = (Book) s.get( Book.class, book.getIsbn() );
assertNotNull( loadedBook.getSummary() );
assertEquals( book.getSummary().getText(), loadedBook.getSummary().getText() );
s.delete( loadedBook );
tx.commit();
s.close();
}
@Test
public void testParent() throws Exception {
Session s;
s = openSession();
s.getTransaction().begin();
Book book = new Book();
book.setIsbn( "1234" );
book.setName( "HiA Second Edition" );
Summary summary = new Summary();
summary.setText( "This is a HiA SE summary" );
summary.setSize( summary.getText().length() );
book.setSummary( summary );
s.persist( book );
s.getTransaction().commit();
s.clear();
Transaction tx = s.beginTransaction();
Book loadedBook = (Book) s.get( Book.class, book.getIsbn() );
assertNotNull( loadedBook.getSummary() );
assertEquals( loadedBook, loadedBook.getSummary().getSummarizedBook() );
s.delete( loadedBook );
tx.commit();
s.close();
}
@Test
public void testEmbeddedAndMultipleManyToOne() throws Exception {
Session s;
s = openSession();
Transaction tx = s.beginTransaction();
CorpType type = new CorpType();
type.setType( "National" );
s.persist( type );
Nationality nat = new Nationality();
nat.setName( "Canadian" );
s.persist( nat );
InternetProvider provider = new InternetProvider();
provider.setBrandName( "Fido" );
LegalStructure structure = new LegalStructure();
structure.setCorporationType( type );
structure.setCountry( "Canada" );
structure.setName( "Rogers" );
provider.setOwner( structure );
structure.setOrigin( nat );
s.persist( provider );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
provider = (InternetProvider) s.get( InternetProvider.class, provider.getId() );
assertNotNull( provider.getOwner() );
assertNotNull( "Many to one not set", provider.getOwner().getCorporationType() );
assertEquals( "Wrong link", type.getType(), provider.getOwner().getCorporationType().getType() );
assertNotNull( "2nd Many to one not set", provider.getOwner().getOrigin() );
assertEquals( "Wrong 2nd link", nat.getName(), provider.getOwner().getOrigin().getName() );
s.delete( provider );
s.delete( provider.getOwner().getCorporationType() );
s.delete( provider.getOwner().getOrigin() );
tx.commit();
s.close();
}
@Test
public void testEmbeddedAndOneToMany() throws Exception {
Session s;
s = openSession();
Transaction tx = s.beginTransaction();
InternetProvider provider = new InternetProvider();
provider.setBrandName( "Fido" );
LegalStructure structure = new LegalStructure();
structure.setCountry( "Canada" );
structure.setName( "Rogers" );
provider.setOwner( structure );
s.persist( provider );
Manager manager = new Manager();
manager.setName( "Bill" );
manager.setEmployer( provider );
structure.getTopManagement().add( manager );
s.persist( manager );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
provider = (InternetProvider) s.get( InternetProvider.class, provider.getId() );
assertNotNull( provider.getOwner() );
Set<Manager> topManagement = provider.getOwner().getTopManagement();
assertNotNull( "OneToMany not set", topManagement );
assertEquals( "Wrong number of elements", 1, topManagement.size() );
manager = topManagement.iterator().next();
assertEquals( "Wrong element", "Bill", manager.getName() );
s.delete( manager );
s.delete( provider );
tx.commit();
s.close();
}
@Test
public void testDefaultCollectionTable() throws Exception {
//are the tables correct?
assertTrue( SchemaUtil.isTablePresent("WealthyPerson_vacationHomes", metadata() ) );
assertTrue( SchemaUtil.isTablePresent("WealthyPerson_legacyVacationHomes", metadata() ) );
assertTrue( SchemaUtil.isTablePresent("WelPers_VacHomes", metadata() ) );
//just to make sure, use the mapping
Session s;
Transaction tx;
WealthyPerson p = new WealthyPerson();
Address a = new Address();
Address vacation = new Address();
Country c = new Country();
Country bornCountry = new Country();
c.setIso2( "DM" );
c.setName( "Matt Damon Land" );
bornCountry.setIso2( "US" );
bornCountry.setName( "United States of America" );
a.address1 = "colorado street";
a.city = "Springfield";
a.country = c;
vacation.address1 = "rock street";
vacation.city = "Plymouth";
vacation.country = c;
p.vacationHomes.add(vacation);
p.address = a;
p.bornIn = bornCountry;
p.name = "Homer";
s = openSession();
tx = s.beginTransaction();
s.persist( p );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
p = (WealthyPerson) s.get( WealthyPerson.class, p.id );
assertNotNull( p );
assertNotNull( p.address );
assertEquals( "Springfield", p.address.city );
assertNotNull( p.address.country );
assertEquals( "DM", p.address.country.getIso2() );
assertNotNull( p.bornIn );
assertEquals( "US", p.bornIn.getIso2() );
tx.commit();
s.close();
}
// make sure we support collection of embeddable objects inside embeddable objects
@Test
public void testEmbeddableInsideEmbeddable() throws Exception {
Session s;
Transaction tx;
Collection<URLFavorite> urls = new ArrayList<URLFavorite>();
URLFavorite urlFavorite = new URLFavorite();
urlFavorite.setUrl( "http://highscalability.com/" );
urls.add(urlFavorite);
urlFavorite = new URLFavorite();
urlFavorite.setUrl( "http://www.jboss.org/" );
urls.add(urlFavorite);
urlFavorite = new URLFavorite();
urlFavorite.setUrl( "http://www.hibernate.org/" );
urls.add(urlFavorite);
urlFavorite = new URLFavorite();
urlFavorite.setUrl( "http://www.jgroups.org/" );
urls.add( urlFavorite );
Collection<String>ideas = new ArrayList<String>();
ideas.add( "lionheart" );
ideas.add( "xforms" );
ideas.add( "dynamic content" );
ideas.add( "http" );
InternetFavorites internetFavorites = new InternetFavorites();
internetFavorites.setLinks( urls );
internetFavorites.setIdeas( ideas );
FavoriteThings favoriteThings = new FavoriteThings();
favoriteThings.setWeb( internetFavorites );
s = openSession();
tx = s.beginTransaction();
s.persist(favoriteThings);
tx.commit();
tx = s.beginTransaction();
s.flush();
favoriteThings = (FavoriteThings) s.get( FavoriteThings.class, favoriteThings.getId() );
assertTrue( "has web", favoriteThings.getWeb() != null );
assertTrue( "has ideas", favoriteThings.getWeb().getIdeas() != null );
assertTrue( "has favorite idea 'http'",favoriteThings.getWeb().getIdeas().contains("http") );
assertTrue( "has favorite idea 'http'",favoriteThings.getWeb().getIdeas().contains("dynamic content") );
urls = favoriteThings.getWeb().getLinks();
assertTrue( "has urls", urls != null);
URLFavorite[] favs = new URLFavorite[4];
urls.toArray(favs);
assertTrue( "has http://www.hibernate.org url favorite link",
"http://www.hibernate.org/".equals( favs[0].getUrl() ) ||
"http://www.hibernate.org/".equals( favs[1].getUrl() ) ||
"http://www.hibernate.org/".equals( favs[2].getUrl() ) ||
"http://www.hibernate.org/".equals( favs[3].getUrl() ));
tx.commit();
s.close();
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[]{
Person.class,
WealthyPerson.class,
RegionalArticle.class,
AddressType.class,
VanillaSwap.class,
SpreadDeal.class,
Book.class,
InternetProvider.class,
CorpType.class,
Nationality.class,
Manager.class,
FavoriteThings.class,
Address.class,
Country.class,
InternetFavorites.class,
FixedLeg.class,
FloatLeg.class,
Swap.class
};
}
}