01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.persistence.query;
20:
21: import java.util.List;
22: import javax.persistence.EntityManager;
23:
24: import org.apache.openjpa.persistence.query.common.apps.RuntimeTest1;
25: import org.apache.openjpa.persistence.query.common.apps.RuntimeTest2;
26:
27: public class TestEJBPolymorphicQuery extends BaseQueryTest {
28:
29: public TestEJBPolymorphicQuery(String name) {
30: super (name);
31: }
32:
33: public void setUp() {
34: deleteAll(RuntimeTest1.class);
35: deleteAll(RuntimeTest2.class);
36:
37: EntityManager em = currentEntityManager();
38: startTx(em);
39:
40: int run1 = 10;
41: int run2 = 15;
42:
43: for (int i = 0; i < run1; i++) {
44: RuntimeTest1 rt = new RuntimeTest1(i);
45: rt.setStringField("foo " + i);
46: em.persist(rt);
47: }
48:
49: for (int i = 10; i < run2; i++) {
50: em.persist(new RuntimeTest2(i));
51: }
52:
53: endTx(em);
54: endEm(em);
55: }
56:
57: /**
58: * Ensures that when a select query is ran against an entity at the top of the hierarchy
59: * that the result is its instances and that of all its subclass.
60: */
61: public void testPolymorphicSelect() {
62: EntityManager em = currentEntityManager();
63:
64: List l = em.createQuery("Select object(o) from RuntimeTest1 o")
65: .getResultList();
66:
67: assertNotNull(l);
68: assertEquals(15, l.size());
69:
70: endEm(em);
71: }
72:
73: public void testPolymorphicDelete() {
74: EntityManager em = currentEntityManager();
75: startTx(em);
76:
77: int l = em.createQuery("Delete from RuntimeTest1")
78: .executeUpdate();
79:
80: assertNotNull(l);
81: assertEquals(15, l);
82:
83: endTx(em);
84: endEm(em);
85: }
86: }
|