KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > referencedcolumnname > ReferencedColumnNameTest


1 //$Id: ReferencedColumnNameTest.java,v 1.3 2005/07/22 02:33:21 epbernard Exp $
2
package org.hibernate.test.annotations.referencedcolumnname;
3
4 import org.hibernate.Session;
5 import org.hibernate.Transaction;
6 import org.hibernate.test.annotations.TestCase;
7
8 /**
9  * @author Emmanuel Bernard
10  */

11 public class ReferencedColumnNameTest extends TestCase {
12     public void testManyToOne() throws Exception JavaDoc {
13         Session s;
14         Transaction tx;
15         s = openSession();
16         tx = s.beginTransaction();
17         Postman pm = new Postman( "Bob", "A01" );
18         House house = new House();
19         house.setPostman( pm );
20         house.setAddress( "Rue des prés" );
21         s.persist( pm );
22         s.persist( house );
23         tx.commit();
24         s.close();
25         s = openSession();
26         tx = s.beginTransaction();
27         house = (House) s.get( House.class, house.getId() );
28         assertNotNull( house.getPostman() );
29         assertEquals( "Bob", house.getPostman().getName() );
30         pm = house.getPostman();
31         s.delete(house);
32         s.delete(pm);
33         tx.commit();
34         s.close();
35     }
36
37     public void testOneToMany() throws Exception JavaDoc {
38         Session s;
39         Transaction tx;
40         s = openSession();
41         tx = s.beginTransaction();
42
43         Rambler rambler = new Rambler("Emmanuel");
44         Bag bag = new Bag("0001", rambler);
45         rambler.getBags().add(bag);
46         s.persist( rambler );
47
48         tx.commit();
49         s.close();
50
51         s = openSession();
52         tx = s.beginTransaction();
53
54         bag = (Bag) s.createQuery( "select b from Bag b left join fetch b.owner" ).uniqueResult();
55         assertNotNull( bag );
56         assertNotNull( bag.getOwner() );
57
58         rambler = (Rambler) s.createQuery( "select r from Rambler r left join fetch r.bags" ).uniqueResult();
59         assertNotNull( rambler );
60         assertNotNull( rambler.getBags() );
61         assertEquals( 1, rambler.getBags().size() );
62         s.delete( rambler.getBags().iterator().next() );
63         s.delete( rambler );
64
65         tx.commit();
66         s.close();
67     }
68
69     public void testUnidirectionalOneToMany() throws Exception JavaDoc {
70         Session s;
71         Transaction tx;
72         s = openSession();
73         tx = s.beginTransaction();
74
75         Clothes clothes = new Clothes("underwear", "interesting");
76         Luggage luggage = new Luggage( "Emmanuel", "Cabin Luggage" );
77         luggage.getHasInside().add(clothes);
78         s.persist( luggage );
79
80         tx.commit();
81         s.close();
82
83         s = openSession();
84         tx = s.beginTransaction();
85
86         luggage = (Luggage) s.createQuery( "select l from Luggage l left join fetch l.hasInside" ).uniqueResult();
87         assertNotNull( luggage );
88         assertNotNull( luggage.getHasInside() );
89         assertEquals( 1, luggage.getHasInside().size() );
90
91         s.delete( luggage.getHasInside().iterator().next() );
92         s.delete( luggage );
93
94         tx.commit();
95         s.close();
96     }
97
98     public ReferencedColumnNameTest(String JavaDoc x) {
99         super( x );
100     }
101
102     protected Class JavaDoc[] getMappings() {
103         return new Class JavaDoc[] {
104             House.class,
105             Postman.class,
106             Bag.class,
107             Rambler.class,
108             Luggage.class,
109             Clothes.class
110         };
111     }
112 }
113
Popular Tags