~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/manytoone/ManyToOneTest.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: ManyToOneTest.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $
2
 
package org.hibernate.test.annotations.manytoone;
3
 
 
4
 
import java.util.ArrayList;
5
 
import java.util.Collection;
6
 
import java.util.List;
7
 
 
8
 
import org.hibernate.Hibernate;
9
 
import org.hibernate.Query;
10
 
import org.hibernate.Session;
11
 
import org.hibernate.Transaction;
12
 
import org.hibernate.test.annotations.Company;
13
 
import org.hibernate.test.annotations.Customer;
14
 
import org.hibernate.test.annotations.Discount;
15
 
import org.hibernate.test.annotations.Flight;
16
 
import org.hibernate.test.annotations.Passport;
17
 
import org.hibernate.test.annotations.TestCase;
18
 
import org.hibernate.test.annotations.Ticket;
19
 
 
20
 
/**
21
 
 * @author Emmanuel Bernard
22
 
 */
23
 
public class ManyToOneTest extends TestCase {
24
 
 
25
 
        public ManyToOneTest(String x) {
26
 
                super( x );
27
 
        }
28
 
 
29
 
        public void testEager() throws Exception {
30
 
                Session s;
31
 
                Transaction tx;
32
 
                s = openSession();
33
 
                tx = s.beginTransaction();
34
 
                Color c = new Color();
35
 
                c.setName( "Yellow" );
36
 
                s.persist( c );
37
 
                Car car = new Car();
38
 
                car.setBodyColor( c );
39
 
                s.persist( car );
40
 
                tx.commit();
41
 
                s.close();
42
 
 
43
 
                s = openSession();
44
 
                tx = s.beginTransaction();
45
 
                car = (Car) s.get( Car.class, car.getId() );
46
 
                tx.commit();
47
 
                s.close();
48
 
                assertNotNull( car );
49
 
                assertNotNull( car.getBodyColor() );
50
 
                assertEquals( "Yellow", car.getBodyColor().getName() );
51
 
 
52
 
        }
53
 
 
54
 
        public void testDefaultMetadata() throws Exception {
55
 
                Session s;
56
 
                Transaction tx;
57
 
                s = openSession();
58
 
                tx = s.beginTransaction();
59
 
                Color c = new Color();
60
 
                c.setName( "Blue" );
61
 
                s.persist( c );
62
 
                Car car = new Car();
63
 
                car.setBodyColor( c );
64
 
                s.persist( car );
65
 
                tx.commit();
66
 
                s.close();
67
 
 
68
 
                s = openSession();
69
 
                tx = s.beginTransaction();
70
 
                car = (Car) s.get( Car.class, car.getId() );
71
 
                assertNotNull( car );
72
 
                assertNotNull( car.getBodyColor() );
73
 
                assertEquals( c.getId(), car.getBodyColor().getId() );
74
 
                tx.rollback();
75
 
                s.close();
76
 
        }
77
 
 
78
 
        public void testCreate() throws Exception {
79
 
                Session s;
80
 
                Transaction tx;
81
 
                s = openSession();
82
 
                tx = s.beginTransaction();
83
 
                Flight firstOne = new Flight();
84
 
                firstOne.setId( new Long( 1 ) );
85
 
                firstOne.setName( "AF0101" );
86
 
                firstOne.setDuration( new Long( 1000 ) );
87
 
                Company frenchOne = new Company();
88
 
                frenchOne.setName( "Air France" );
89
 
                firstOne.setCompany( frenchOne );
90
 
                s.persist( firstOne );
91
 
                tx.commit();
92
 
                s.close();
93
 
                assertNotNull( "identity id should work", frenchOne.getId() );
94
 
 
95
 
                s = openSession();
96
 
                tx = s.beginTransaction();
97
 
                firstOne = (Flight) s.get( Flight.class, new Long( 1 ) );
98
 
                assertNotNull( firstOne.getCompany() );
99
 
                assertEquals( frenchOne.getName(), firstOne.getCompany().getName() );
100
 
                tx.commit();
101
 
                s.close();
102
 
        }
103
 
 
104
 
        public void testCascade() throws Exception {
105
 
                Session s;
106
 
                Transaction tx;
107
 
                s = openSession();
108
 
                tx = s.beginTransaction();
109
 
                Discount discount = new Discount();
110
 
                discount.setDiscount( 20.12 );
111
 
                Customer customer = new Customer();
112
 
                Collection discounts = new ArrayList();
113
 
                discounts.add( discount );
114
 
                customer.setName( "Quentin Tarantino" );
115
 
                discount.setOwner( customer );
116
 
                customer.setDiscountTickets( discounts );
117
 
                s.persist( discount );
118
 
                tx.commit();
119
 
                s.close();
120
 
 
121
 
                s = openSession();
122
 
                tx = s.beginTransaction();
123
 
                discount = (Discount) s.get( Discount.class, discount.getId() );
124
 
                assertNotNull( discount );
125
 
                assertEquals( 20.12, discount.getDiscount() );
126
 
                assertNotNull( discount.getOwner() );
127
 
                customer = new Customer();
128
 
                customer.setName( "Clooney" );
129
 
                discount.setOwner( customer );
130
 
                discounts = new ArrayList();
131
 
                discounts.add( discount );
132
 
                customer.setDiscountTickets( discounts );
133
 
                tx.commit();
134
 
                s.close();
135
 
 
136
 
                s = openSession();
137
 
                tx = s.beginTransaction();
138
 
                discount = (Discount) s.get( Discount.class, discount.getId() );
139
 
                assertNotNull( discount );
140
 
                assertNotNull( discount.getOwner() );
141
 
                assertEquals( "Clooney", discount.getOwner().getName() );
142
 
                tx.commit();
143
 
                s.close();
144
 
 
145
 
                s = openSession();
146
 
                tx = s.beginTransaction();
147
 
                customer = (Customer) s.get( Customer.class, customer.getId() );
148
 
                s.delete( customer );
149
 
                tx.commit();
150
 
                s.close();
151
 
        }
152
 
 
153
 
        public void testFetch() throws Exception {
154
 
                Session s;
155
 
                Transaction tx;
156
 
                s = openSession();
157
 
                tx = s.beginTransaction();
158
 
                Discount discount = new Discount();
159
 
                discount.setDiscount( 20 );
160
 
                Customer customer = new Customer();
161
 
                Collection discounts = new ArrayList();
162
 
                discounts.add( discount );
163
 
                customer.setName( "Quentin Tarantino" );
164
 
                discount.setOwner( customer );
165
 
                customer.setDiscountTickets( discounts );
166
 
                s.persist( discount );
167
 
                tx.commit();
168
 
                s.close();
169
 
 
170
 
                s = openSession();
171
 
                tx = s.beginTransaction();
172
 
                discount = (Discount) s.get( Discount.class, discount.getId() );
173
 
                assertNotNull( discount );
174
 
                assertFalse( Hibernate.isInitialized( discount.getOwner() ) );
175
 
                tx.commit();
176
 
 
177
 
                s = openSession();
178
 
                tx = s.beginTransaction();
179
 
                discount = (Discount) s.load( Discount.class, discount.getId() );
180
 
                assertNotNull( discount );
181
 
                assertFalse( Hibernate.isInitialized( discount.getOwner() ) );
182
 
                tx.commit();
183
 
 
184
 
                s = openSession();
185
 
                tx = s.beginTransaction();
186
 
                s.delete( s.get( Discount.class, discount.getId() ) );
187
 
                tx.commit();
188
 
                s.close();
189
 
        }
190
 
 
191
 
        public void testCompositeFK() throws Exception {
192
 
                Session s;
193
 
                Transaction tx;
194
 
                s = openSession();
195
 
                tx = s.beginTransaction();
196
 
                ParentPk ppk = new ParentPk();
197
 
                ppk.firstName = "John";
198
 
                ppk.lastName = "Doe";
199
 
                Parent p = new Parent();
200
 
                p.age = 45;
201
 
                p.id = ppk;
202
 
                s.persist( p );
203
 
                Child c = new Child();
204
 
                c.parent = p;
205
 
                s.persist( c );
206
 
                tx.commit();
207
 
                s.close();
208
 
 
209
 
                s = openSession();
210
 
                tx = s.beginTransaction();
211
 
                //FIXME: fix this when the small parser bug will be fixed 
212
 
                Query q = s.createQuery( "from " + Child.class.getName() ); //+ " c where c.parent.id.lastName = :lastName");
213
 
                //q.setString("lastName", p.id.lastName);
214
 
                List result = q.list();
215
 
                assertEquals( 1, result.size() );
216
 
                Child c2 = (Child) result.get( 0 );
217
 
                assertEquals( c2.id, c.id );
218
 
                tx.commit();
219
 
                s.close();
220
 
        }
221
 
 
222
 
        public void testImplicitCompositeFk() throws Exception {
223
 
                Session s;
224
 
                Transaction tx;
225
 
                s = openSession();
226
 
                tx = s.beginTransaction();
227
 
                Node n1 = new Node();
228
 
                n1.setDescription( "Parent" );
229
 
                NodePk n1pk = new NodePk();
230
 
                n1pk.setLevel( 1 );
231
 
                n1pk.setName( "Root" );
232
 
                n1.setId( n1pk );
233
 
                Node n2 = new Node();
234
 
                NodePk n2pk = new NodePk();
235
 
                n2pk.setLevel( 2 );
236
 
                n2pk.setName( "Level 1: A" );
237
 
                n2.setParent( n1 );
238
 
                n2.setId( n2pk );
239
 
                s.persist( n2 );
240
 
                tx.commit();
241
 
 
242
 
                s = openSession();
243
 
                tx = s.beginTransaction();
244
 
                n2 = (Node) s.get( Node.class, n2pk );
245
 
                assertNotNull( n2 );
246
 
                assertNotNull( n2.getParent() );
247
 
                assertEquals( 1, n2.getParent().getId().getLevel() );
248
 
                tx.commit();
249
 
                s.close();
250
 
        }
251
 
 
252
 
        public void testManyToOneAndIdClass() throws Exception {
253
 
 
254
 
        }
255
 
 
256
 
        public void testManyToOneNonPk() throws Exception {
257
 
                Session s = openSession();
258
 
                Transaction tx = s.beginTransaction();
259
 
                Order order = new Order();
260
 
                order.setOrderNbr( "123" );
261
 
                s.persist( order );
262
 
                OrderLine ol = new OrderLine();
263
 
                ol.setItem( "Mouse" );
264
 
                ol.setOrder( order );
265
 
                s.persist( ol );
266
 
                s.flush();
267
 
                s.clear();
268
 
                ol = (OrderLine) s.get( OrderLine.class, ol.getId() );
269
 
                assertNotNull( ol.getOrder() );
270
 
                assertEquals( "123", ol.getOrder().getOrderNbr() );
271
 
                assertTrue( ol.getOrder().getOrderLines().contains( ol ) );
272
 
                tx.rollback();
273
 
                s.close();
274
 
        }
275
 
 
276
 
        public void testTwoManyToOneNonPk() throws Exception {
277
 
                //2 many to one non pk pointing to the same referencedColumnName should not fail
278
 
                Session s = openSession();
279
 
                Transaction tx = s.beginTransaction();
280
 
                org.hibernate.test.annotations.manytoone.Customer customer = new org.hibernate.test.annotations.manytoone.Customer();
281
 
                customer.userId="123";
282
 
                org.hibernate.test.annotations.manytoone.Customer customer2 = new org.hibernate.test.annotations.manytoone.Customer();
283
 
                customer2.userId="124";
284
 
                s.persist( customer2 );
285
 
                s.persist( customer );
286
 
                Deal deal = new Deal();
287
 
                deal.from = customer;
288
 
                deal.to = customer2;
289
 
                s.persist( deal );
290
 
                s.flush();
291
 
                s.clear();
292
 
                deal = (Deal) s.get( Deal.class, deal.id );
293
 
                assertNotNull( deal.from );
294
 
                assertNotNull( deal.to );
295
 
                tx.rollback();
296
 
                s.close();
297
 
        }
298
 
 
299
 
        public void testFormulaOnOtherSide() throws Exception {
300
 
                Session s = openSession();
301
 
                Transaction tx = s.beginTransaction();
302
 
                Frame frame = new Frame();
303
 
                frame.setName( "Prada" );
304
 
                s.persist( frame );
305
 
                Lens l = new Lens();
306
 
                l.setFocal( 2.5f );
307
 
                l.setFrame( frame );
308
 
                s.persist( l );
309
 
                Lens r = new Lens();
310
 
                r.setFocal( 1.2f);
311
 
                r.setFrame( frame );
312
 
                s.persist( r );
313
 
                s.flush();
314
 
                s.clear();
315
 
                frame = (Frame) s.get( Frame.class, frame.getId() );
316
 
                assertEquals( 2, frame.getLenses().size() );
317
 
                assertTrue( frame.getLenses().iterator().next().getLength() <= 1/1.2f );
318
 
                assertTrue( frame.getLenses().iterator().next().getLength() >= 1/2.5f );
319
 
                tx.rollback();
320
 
                s.close();
321
 
        }
322
 
 
323
 
        /**
324
 
         * @see org.hibernate.test.annotations.TestCase#getMappings()
325
 
         */
326
 
        protected java.lang.Class[] getMappings() {
327
 
                return new java.lang.Class[]{
328
 
                                Deal.class,
329
 
                                org.hibernate.test.annotations.manytoone.Customer.class,
330
 
                                Car.class,
331
 
                                Color.class,
332
 
                                Flight.class,
333
 
                                Company.class,
334
 
                                Customer.class,
335
 
                                Discount.class,
336
 
                                Ticket.class,
337
 
                                Passport.class,
338
 
                                Parent.class,
339
 
                                Child.class,
340
 
                                Node.class,
341
 
                                User.class,
342
 
                                DistrictUser.class,
343
 
                                Order.class,
344
 
                                OrderLine.class,
345
 
                                Frame.class,
346
 
                                Lens.class
347
 
                };
348
 
        }
349
 
 
350
 
}
 
 
b'\\ No newline at end of file'