KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > OQLOrOnForeignKeyTest


1 package org.apache.ojb.odmg;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.ojb.junit.ODMGTestCase;
23 import org.apache.ojb.odmg.shared.PersonImpl;
24 import org.apache.ojb.odmg.shared.TestClassA;
25 import org.apache.ojb.odmg.shared.TestClassB;
26 import org.odmg.OQLQuery;
27 import org.odmg.Transaction;
28
29 /**
30  * @author Matthew.Baird
31  *
32  * Illustrates a problem with OJB SQL Generation:
33  *
34  * 1. OJB will generate the following SQL when items are mapped to the same table:
35  *
36  * SELECT A0.FATHER_ID,A0.MOTHER_ID,A0.LASTNAME,A0.FIRSTNAME,A0.ID
37  * FROM FAMILY_MEMBER A0
38  * INNER JOIN FAMILY_MEMBER A2 ON A0.FATHER_ID=A2.ID
39  * INNER JOIN FAMILY_MEMBER A1 ON A0.MOTHER_ID=A1.ID
40  * WHERE A1.ID = ? OR (A2.ID = ? )
41  *
42  * When it should generate:
43  * SELECT A0.FATHER_ID,A0.MOTHER_ID,A0.LASTNAME,A0.FIRSTNAME,A0.ID
44  * FROM FAMILY_MEMBER A0
45  * WHERE A0.FATHER_ID = ? OR (A0.MOTHER_ID = ? )
46  *
47  * or:
48  * SELECT A0.FATHER_ID,A0.MOTHER_ID,A0.LASTNAME,A0.FIRSTNAME,A0.ID
49  * FROM FAMILY_MEMBER A0
50  * LEFT OUTER JOIN FAMILY_MEMBER A1 ON A0.MOTHER_ID=A1.ID
51  * LEFT OUTER JOIN FAMILY_MEMBER A2 ON A0.FATHER_ID=A2.ID
52  * WHERE A1.ID = ? OR (A2.ID = ?)
53  *
54  */

55 public class OQLOrOnForeignKeyTest extends ODMGTestCase
56 {
57     public static void main(String JavaDoc[] args)
58     {
59         String JavaDoc[] arr = {OQLOrOnForeignKeyTest.class.getName()};
60         junit.textui.TestRunner.main(arr);
61     }
62
63     public OQLOrOnForeignKeyTest(String JavaDoc name)
64     {
65         super(name);
66     }
67
68     private void deleteData(Class JavaDoc target)
69             throws Exception JavaDoc
70     {
71         Transaction tx = odmg.newTransaction();
72         tx.begin();
73         OQLQuery query = odmg.newOQLQuery();
74         query.create("select allStuff from " + target.getName());
75         Collection JavaDoc allTargets = (Collection JavaDoc) query.execute();
76         Iterator JavaDoc it = allTargets.iterator();
77         while (it.hasNext())
78         {
79             database.deletePersistent(it.next());
80         }
81         tx.commit();
82     }
83
84     /**
85      * test joins on same table
86      *
87      * @throws Exception
88      */

89     public void testOrReferenceOnSameTable() throws Exception JavaDoc
90     {
91         deleteData(PersonImpl.class);
92
93         PersonImpl jimmy = new PersonImpl();
94         PersonImpl joe = new PersonImpl();
95         PersonImpl father = new PersonImpl();
96         PersonImpl mother = new PersonImpl();
97         OQLQuery query;
98         List JavaDoc persons;
99
100         mother.setFirstname("mom");
101         father.setFirstname("dad");
102
103         jimmy.setMother(mother);
104         jimmy.setFirstname("jimmy");
105
106         joe.setFather(father);
107         joe.setFirstname("joe");
108
109         Transaction tx = odmg.newTransaction();
110         tx.begin();
111         database.makePersistent(father);
112         database.makePersistent(mother);
113         database.makePersistent(jimmy);
114         database.makePersistent(joe);
115         tx.commit();
116
117         // read using id
118
tx = odmg.newTransaction();
119         tx.begin();
120         query = odmg.newOQLQuery();
121         query.create("select person from " + PersonImpl.class.getName() +
122                      " where (mother.id=$1 or father.id=$2)");
123         query.bind(new Integer JavaDoc(mother.getId()));
124         query.bind(new Integer JavaDoc(father.getId()));
125         persons = (List JavaDoc) query.execute();
126         assertEquals(2, persons.size());
127         tx.commit();
128
129         // read using firstname
130
tx = odmg.newTransaction();
131         tx.begin();
132         query = odmg.newOQLQuery();
133         query.create("select person from " + PersonImpl.class.getName() +
134                      " where (mother.firstname=$1 or father.firstname=$2)");
135         query.bind("mom");
136         query.bind("dad");
137         persons = (List JavaDoc) query.execute();
138         assertEquals(2, persons.size());
139         tx.commit();
140     }
141
142     public void testOrReferenceOnDifferentTables() throws Exception JavaDoc
143     {
144         deleteData(TestClassA.class);
145         deleteData(TestClassB.class);
146
147         TestClassA a1 = new TestClassA();
148         TestClassA a2 = new TestClassA();
149
150         TestClassB b1 = new TestClassB();
151         TestClassB b2 = new TestClassB();
152         a1.setB(b1);
153         a2.setB(b2);
154
155         Transaction tx = odmg.newTransaction();
156         tx.begin();
157         database.makePersistent(a1);
158         database.makePersistent(a2);
159         database.makePersistent(b1);
160         database.makePersistent(b2);
161         tx.commit();
162         tx = odmg.newTransaction();
163         tx.begin();
164         // get the right values
165
OQLQuery query = odmg.newOQLQuery();
166         query.create("select a from " + TestClassA.class.getName());
167         List JavaDoc As = (List JavaDoc) query.execute();
168         Iterator JavaDoc asIterator = As.iterator();
169         TestClassA temp = null;
170
171         temp = (TestClassA) asIterator.next();
172         String JavaDoc bID1 = temp.getB().getOid();
173         temp = (TestClassA) asIterator.next();
174         String JavaDoc bID2 = temp.getB().getOid();
175
176         query = odmg.newOQLQuery();
177         query.create("select a from " + TestClassA.class.getName() +
178                      " where (b.oid=$1 or b.oid=$2)");
179         query.bind(bID1);
180         query.bind(bID2);
181         As = (List JavaDoc) query.execute();
182         assertTrue(As.size() == 2);
183         tx.commit();
184     }
185 }
186
Popular Tags