KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > annotations > embedded > EmbeddedTest


1 //$Id: EmbeddedTest.java,v 1.1 2005/05/12 13:33:32 epbernard Exp $
2
package org.hibernate.test.annotations.embedded;
3
4 import org.hibernate.Query;
5 import org.hibernate.Session;
6 import org.hibernate.Transaction;
7 import org.hibernate.test.annotations.TestCase;
8
9 import java.io.Serializable JavaDoc;
10 import java.util.List JavaDoc;
11
12 /**
13  * @author Emmanuel Bernard
14  */

15 public class EmbeddedTest extends TestCase {
16
17     public void testSimple() throws Exception JavaDoc {
18         Session s;
19         Transaction tx;
20         Person p = new Person();
21         Address a = new Address();
22         Country c = new Country();
23         Country bornCountry = new Country();
24         c.setIso2("DM");
25         c.setName("Matt Damon Land");
26         bornCountry.setIso2("US");
27         bornCountry.setName("United States of America");
28
29         a.address1 = "colorado street";
30         a.city = "Springfield";
31         a.country = c;
32         p.address = a;
33         p.bornIn = bornCountry;
34         p.name = "Homer";
35         s = openSession();
36         tx = s.beginTransaction();
37         s.persist(p);
38         tx.commit();
39         s.close();
40
41         s = openSession();
42         tx = s.beginTransaction();
43         p = (Person) s.get(Person.class, p.id);
44         assertNotNull(p);
45         assertNotNull(p.address);
46         assertEquals("Springfield", p.address.city);
47         assertNotNull(p.address.country);
48         assertEquals( "DM", p.address.country.getIso2() );
49         assertNotNull(p.bornIn);
50         assertEquals( "US", p.bornIn.getIso2() );
51         tx.commit();
52         s.close();
53     }
54
55     public void testCompositeId() throws Exception JavaDoc {
56         Session s;
57         Transaction tx;
58         RegionalArticlePk pk = new RegionalArticlePk();
59         pk.iso2 = "FR";
60         pk.localUniqueKey = "1234567890123";
61         RegionalArticle reg = new RegionalArticle();
62         reg.setName("Je ne veux pes rester sage - Dolly");
63         reg.setPk(pk);
64         s = openSession();
65         tx = s.beginTransaction();
66         s.persist(reg);
67         tx.commit();
68         s.close();
69
70         s = openSession();
71         tx = s.beginTransaction();
72         reg = (RegionalArticle) s.get( RegionalArticle.class, (Serializable JavaDoc) reg.getPk() );
73         assertNotNull(reg);
74         assertNotNull( reg.getPk() );
75         assertEquals( "Je ne veux pes rester sage - Dolly", reg.getName() );
76         assertEquals("FR", reg.getPk().iso2);
77         tx.commit();
78         s.close();
79     }
80
81     public void testManyToOneInsideComponent() throws Exception JavaDoc {
82         Session s;
83         Transaction tx;
84         s = openSession();
85         tx = s.beginTransaction();
86         Person p = new Person();
87         Country bornIn = new Country();
88         bornIn.setIso2("FR");
89         bornIn.setName("France");
90         p.bornIn = bornIn;
91         p.name = "Emmanuel";
92         AddressType type = new AddressType();
93         type.setName("Primary Home");
94         s.persist(type);
95         Country currentCountry = new Country();
96         currentCountry.setIso2("US");
97         currentCountry.setName("USA");
98         Address add = new Address();
99         add.address1 = "4 square street";
100         add.city = "San diego";
101         add.country = currentCountry;
102         add.type = type;
103         p.address = add;
104         s.persist(p);
105         tx.commit();
106
107         s = openSession();
108         tx = s.beginTransaction();
109         Query q = s.createQuery("select p from Person p where p.address.city = :city");
110         q.setString("city", add.city);
111         List JavaDoc result = q.list();
112         Person samePerson = (Person) result.get(0);
113         assertNotNull(samePerson.address.type);
114         assertEquals( type.getName(), samePerson.address.type.getName() );
115         tx.commit();
116         s.close();
117     }
118
119     public EmbeddedTest(String JavaDoc x) {
120         super(x);
121     }
122
123     protected Class JavaDoc[] getMappings() {
124         return new Class JavaDoc[] {
125             Person.class,
126             RegionalArticle.class,
127             AddressType.class
128         };
129     }
130 }
131
Popular Tags