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.jpql.joins;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023: import javax.persistence.EntityManager;
024:
025: import org.apache.openjpa.persistence.common.apps.ArtCourse;
026: import org.apache.openjpa.persistence.common.apps.Course;
027: import org.apache.openjpa.persistence.common.apps.Department;
028: import org.apache.openjpa.persistence.common.apps.Student;
029: import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
030:
031: public class TestEJBJoins extends AbstractTestCase {
032:
033: public TestEJBJoins(String name) {
034: super (name, "jpqlclausescactusapp");
035: }
036:
037: public void setUp() {
038: deleteAll(Course.class);
039: deleteAll(Student.class);
040: deleteAll(Department.class);
041:
042: EntityManager em = currentEntityManager();
043: startTx(em);
044:
045: String name = "";
046: List<Course> clist = new ArrayList<Course>();
047: List<Department> dlist = new ArrayList<Department>();
048:
049: for (int i = 0; i < 5; i++) {
050: Course curr = new Course("Math " + i, i * 2, i);
051: Course acurr = new ArtCourse(i + 20, "English" + (2 * i));
052: Department durr = new Department("CompSci" + i, null, i + 2);
053: clist.add(curr);
054: clist.add(acurr);
055: dlist.add(durr);
056: }
057:
058: Student stud = new Student("Jonathan", clist, dlist);
059: Student stud2 = new Student("Stam", null, dlist);
060: Student stud3 = new Student("John", clist, null);
061: Student stud4 = new Student("Bill", null, null);
062:
063: em.persist(stud);
064: em.persist(stud2);
065: em.persist(stud3);
066: em.persist(stud4);
067:
068: endTx(em);
069: endEm(em);
070: }
071:
072: public void testInnerJoin() {
073: EntityManager em = currentEntityManager();
074: String query = "SELECT distinct o.name from Student o JOIN "
075: + "o.course d WHERE d.name" + "='Math 4'";
076:
077: List ls = (List) em.createQuery(query).getResultList();
078:
079: assertNotNull(ls);
080:
081: if (ls != null) {
082: assertEquals(2, ls.size());
083: }
084: endEm(em);
085: }
086:
087: public void testOuterJoin() {
088: EntityManager em = currentEntityManager();
089: String query = "SELECT distinct s.name FROM Student "
090: + "s LEFT JOIN s.department d";
091:
092: List ls = (List) em.createQuery(query).getResultList();
093:
094: assertNotNull(ls);
095: assertEquals(4, ls.size());
096:
097: endEm(em);
098: }
099:
100: public void testFetchJoin1() {
101: EntityManager em = currentEntityManager();
102: String query = "SELECT s FROM Student s JOIN FETCH s.name";
103:
104: List ls = em.createQuery(query).getResultList();
105:
106: assertNotNull(ls);
107: assertEquals(4, ls.size());
108:
109: endEm(em);
110: }
111:
112: public void testFetchJoin2() {
113: EntityManager em = currentEntityManager();
114: String query = "SELECT s " + "FROM Student s "
115: + "JOIN FETCH s.name d";
116:
117: try {
118: List ls = em.createQuery(query).getResultList();
119: fail("Not permitted to specify an id variable for entities ref. by the right side of fetch join");
120: } catch (Exception e) {
121: //suppose to throw an exception..should not pass
122: }
123:
124: endEm(em);
125: }
126:
127: public void testLeftOuterJoin() {
128: EntityManager em = currentEntityManager();
129:
130: String ljoin = "SELECT DISTINCT s.name FROM Student s LEFT OUTER JOIN s.department d "
131: + "WHERE d.name = 'CompSci2'";
132:
133: List ls = em.createQuery(ljoin).getResultList();
134:
135: assertNotNull(ls);
136: assertEquals(2, ls.size());
137:
138: assertTrue(ls.contains("Jonathan"));
139: assertTrue(ls.contains("Stam"));
140:
141: endEm(em);
142: }
143: }
|