001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.generationtype;
020:
021: import java.util.List;
022: import javax.persistence.EntityManager;
023: import javax.persistence.Query;
024: import org.apache.openjpa.persistence.*;
025: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
026:
027: public class TestMultipleSchemaNames extends SingleEMFTestCase {
028: public void setUp() {
029: setUp(Dog1.class, Dog2.class, DogTable.class, DogTable2.class);
030:
031: EntityManager em = emf.createEntityManager();
032: em.getTransaction().begin();
033: // cleanup database first
034: Query qry = em.createQuery("select d from Dog1 d");
035: List result = qry.getResultList();
036:
037: for (int index = 0; index < result.size(); index++) {
038: Dog1 Obj = (Dog1) result.get(index);
039: em.remove(Obj);
040: }
041: Query qry2 = em.createQuery("select d from Dog2 d");
042: List result2 = qry2.getResultList();
043:
044: for (int index = 0; index < result2.size(); index++) {
045: Dog2 Obj = (Dog2) result2.get(index);
046: em.remove(Obj);
047: }
048: Query qry3 = em.createQuery("select d from DogTable d");
049: List result3 = qry3.getResultList();
050:
051: for (int index = 0; index < result3.size(); index++) {
052: DogTable Obj = (DogTable) result3.get(index);
053: em.remove(Obj);
054: }
055: Query qry4 = em.createQuery("select d from DogTable2 d");
056: List result4 = qry4.getResultList();
057:
058: for (int index = 0; index < result4.size(); index++) {
059: DogTable2 Obj = (DogTable2) result4.get(index);
060: em.remove(Obj);
061: }
062:
063: Query delschema1 = em
064: .createNativeQuery("delete from schema1.openjpa_sequence_table");
065: delschema1.executeUpdate();
066: Query delschema2 = em
067: .createNativeQuery("delete from schema2.openjpa_sequence_table");
068: delschema2.executeUpdate();
069: Query delgentable = em
070: .createNativeQuery("delete from schema1.id_gen1");
071: delgentable.executeUpdate();
072: Query delgentable2 = em
073: .createNativeQuery("delete from schema2.id_gen2");
074: delgentable2.executeUpdate();
075:
076: em.getTransaction().commit();
077:
078: }
079:
080: public void testGeneratedAUTO() {
081: EntityManager em = emf.createEntityManager();
082: OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);
083: em.getTransaction().begin();
084:
085: Dog1 dog1 = new Dog1();
086: dog1.setName("helloDog1");
087: dog1.setPrice(12000);
088:
089: em.persist(dog1);
090: Dog1 dog1a = new Dog1();
091: dog1a.setName("helloDog2");
092: dog1a.setPrice(22000);
093: em.persist(dog1a);
094: // add dog2
095: Dog2 dog2 = new Dog2();
096: dog2.setName("helloDog3");
097: dog2.setPrice(15000);
098: em.persist(dog2);
099:
100: Dog2 dog2a = new Dog2();
101: dog2a.setName("helloDog4");
102: dog2a.setPrice(25000);
103: em.persist(dog2a);
104: em.getTransaction().commit();
105:
106: Dog1 dog1x = em.find(Dog1.class, kem.getObjectId(dog1));
107: // Derby can't guarantee the order of the generated value, therefore,
108: // we can't assert the id based on the order. For db2, we see the id
109: // value in the right order
110: assertTrue(dog1x.getId2() == 1 || dog1x.getId2() == 2);
111: assertEquals(dog1x.getName(), "helloDog1");
112: dog1x.setName("Dog1");
113: dog1x.setDomestic(true);
114: Dog1 dog11 = em.find(Dog1.class, kem.getObjectId(dog1a));
115: assertTrue(dog11.getId2() == 1 || dog11.getId2() == 2);
116: assertEquals(dog11.getName(), "helloDog2");
117: dog11.setName("Dog2");
118: dog11.setDomestic(true);
119: // update dog2
120: Dog2 dog2x = em.find(Dog2.class, kem.getObjectId(dog2));
121: assertTrue(dog2x.getId2() == 1 || dog2x.getId2() == 2);
122: assertEquals(dog2x.getName(), "helloDog3");
123: dog2x.setName("Dog3");
124: dog2x.setDomestic(true);
125: Dog2 dog21 = em.find(Dog2.class, kem.getObjectId(dog2a));
126: assertTrue(dog21.getId2() == 1 || dog21.getId2() == 2);
127: assertEquals(dog21.getName(), "helloDog4");
128: dog21.setName("Dog4");
129: dog21.setDomestic(true);
130:
131: // get the update dog name
132:
133: em.getTransaction().begin();
134: Query qry1 = em
135: .createQuery("select d from Dog1 d order by d.name");
136: List result1 = qry1.getResultList();
137: for (int index = 0; index < result1.size(); index++) {
138: Dog1 dog4 = (Dog1) result1.get(index);
139: int i = index + 1;
140: assertTrue(dog4.getId2() == 1 || dog4.getId2() == 2);
141: assertEquals(dog4.getName(), "Dog" + i);
142: }
143:
144: Query qry2 = em
145: .createQuery("select d from Dog2 d order by d.name");
146: List result2 = qry2.getResultList();
147:
148: for (int index = 0; index < result2.size(); index++) {
149: Dog2 dog5 = (Dog2) result2.get(index);
150: assertTrue(dog5.getId2() == 1 || dog5.getId2() == 2);
151: int j = index + 3;
152: assertEquals(dog5.getName(), "Dog" + j);
153: }
154:
155: em.getTransaction().commit();
156: em.close();
157: }
158:
159: public void testGeneratedTABLE() {
160: EntityManager em = emf.createEntityManager();
161: OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);
162: em.getTransaction().begin();
163:
164: DogTable dog1 = new DogTable();
165: dog1.setName("helloDog1");
166: dog1.setPrice(12000);
167:
168: em.persist(dog1);
169: DogTable dog1a = new DogTable();
170: dog1a.setName("helloDog2");
171: dog1a.setPrice(22000);
172: em.persist(dog1a);
173: // add dog2
174: DogTable2 dog2 = new DogTable2();
175: dog2.setName("helloDog3");
176: dog2.setPrice(15000);
177: em.persist(dog2);
178:
179: DogTable2 dog2a = new DogTable2();
180: dog2a.setName("helloDog4");
181: dog2a.setPrice(25000);
182: em.persist(dog2a);
183: em.getTransaction().commit();
184:
185: DogTable dog1x = em.find(DogTable.class, kem.getObjectId(dog1));
186: assertTrue(dog1x.getId2() == 20 || dog1x.getId2() == 21);
187: assertEquals(dog1x.getName(), "helloDog1");
188: dog1x.setName("Dog1");
189: dog1x.setDomestic(true);
190: DogTable dog11 = em
191: .find(DogTable.class, kem.getObjectId(dog1a));
192: assertTrue(dog11.getId2() == 20 || dog11.getId2() == 21);
193: assertEquals(dog11.getName(), "helloDog2");
194: dog11.setName("Dog2");
195: dog11.setDomestic(true);
196: // update dog2
197: DogTable2 dog2x = em.find(DogTable2.class, kem
198: .getObjectId(dog2));
199: assertTrue(dog2x.getId2() == 100 || dog2x.getId2() == 101);
200: assertEquals(dog2x.getName(), "helloDog3");
201: dog2x.setName("Dog3");
202: dog2x.setDomestic(true);
203: DogTable2 dog21 = em.find(DogTable2.class, kem
204: .getObjectId(dog2a));
205: assertTrue(dog2x.getId2() == 100 || dog2x.getId2() == 101);
206: assertEquals(dog21.getName(), "helloDog4");
207: dog21.setName("Dog4");
208: dog21.setDomestic(true);
209:
210: // get the update dog name
211:
212: em.getTransaction().begin();
213: Query qry1 = em
214: .createQuery("select d from DogTable d order by d.name");
215: List result1 = qry1.getResultList();
216: for (int index = 0; index < result1.size(); index++) {
217: DogTable dog4 = (DogTable) result1.get(index);
218: assertTrue(dog4.getId2() == 20 || dog4.getId2() == 21);
219: int j = index + 1;
220: assertEquals(dog4.getName(), "Dog" + j);
221:
222: }
223:
224: Query qry2 = em
225: .createQuery("select d from DogTable2 d order by d.name");
226: List result2 = qry2.getResultList();
227:
228: for (int index = 0; index < result2.size(); index++) {
229: DogTable2 dog5 = (DogTable2) result2.get(index);
230: assertTrue(dog5.getId2() == 100 || dog5.getId2() == 101);
231: int j = index + 3;
232: assertEquals(dog5.getName(), "Dog" + j);
233: }
234:
235: em.getTransaction().commit();
236: em.close();
237: }
238: } // end of TestMultipleSchemaNames
|