KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > manytoone > ManyToOneTest


1 //$Id: ManyToOneTest.java,v 1.4 2005/07/12 19:27:05 epbernard Exp $
2
package org.hibernate.test.annotations.manytoone;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.List JavaDoc;
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 JavaDoc x) {
26         super(x);
27     }
28
29     public void testEager() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
79         Session s;
80         Transaction tx;
81         s = openSession();
82         tx = s.beginTransaction();
83         Flight firstOne = new Flight();
84         firstOne.setId( new Long JavaDoc(1) );
85         firstOne.setName("AF0101");
86         firstOne.setDuration( new Long JavaDoc(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 JavaDoc(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 JavaDoc {
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 JavaDoc discounts = new ArrayList JavaDoc();
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 JavaDoc();
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 JavaDoc {
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 JavaDoc discounts = new ArrayList JavaDoc();
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         s.close();
177     }
178
179     public void testCompositeFK() throws Exception JavaDoc {
180         Session s;
181         Transaction tx;
182         s = openSession();
183         tx = s.beginTransaction();
184         ParentPk ppk = new ParentPk();
185         ppk.firstName = "John";
186         ppk.lastName = "Doe";
187         Parent p = new Parent();
188         p.age = 45;
189         p.id = ppk;
190         s.persist(p);
191         Child c = new Child();
192         c.parent = p;
193         s.persist(c);
194         tx.commit();
195         s.close();
196
197         s = openSession();
198         tx = s.beginTransaction();
199         //FIXME: fix this when the small parser bug will be fixed
200
Query q = s.createQuery("from " + Child.class.getName() ); //+ " c where c.parent.id.lastName = :lastName");
201
//q.setString("lastName", p.id.lastName);
202
List JavaDoc result = q.list();
203         assertEquals( 1, result.size() );
204         Child c2 = (Child) result.get(0);
205         assertEquals(c2.id, c.id);
206         tx.commit();
207         s.close();
208     }
209
210     public void testImplicitCompositeFk() throws Exception JavaDoc {
211         Session s;
212         Transaction tx;
213         s = openSession();
214         tx = s.beginTransaction();
215         Node n1 = new Node();
216         n1.setDescription("Parent");
217         NodePk n1pk = new NodePk();
218         n1pk.setLevel(1);
219         n1pk.setName("Root");
220         n1.setId(n1pk);
221         Node n2 = new Node();
222         NodePk n2pk = new NodePk();
223         n2pk.setLevel(2);
224         n2pk.setName("Level 1: A");
225         n2.setParent(n1);
226         n2.setId(n2pk);
227         s.persist(n2);
228         tx.commit();
229
230         s = openSession();
231         tx = s.beginTransaction();
232         n2 = (Node) s.get(Node.class, n2pk);
233         assertNotNull(n2);
234         assertNotNull(n2.getParent());
235         assertEquals(1, n2.getParent().getId().getLevel() );
236         tx.commit();
237         s.close();
238     }
239
240     /**
241      * @see org.hibernate.test.annotations.TestCase#getMappings()
242      */

243     protected Class JavaDoc[] getMappings() {
244         return new Class JavaDoc[] {
245             Car.class,
246             Color.class,
247             Flight.class,
248             Company.class,
249             Customer.class,
250             Discount.class,
251             Ticket.class,
252             Passport.class,
253             Parent.class,
254             Child.class,
255             Node.class
256         };
257     }
258
259 }
Popular Tags