mirror of https://github.com/apache/openjpa.git
OPENJPA-1623 Add tests for qualified paths in JPQL
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@932487 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
cfe2a0d236
commit
aefe784c94
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.openjpa.persistence.jdbc.maps.qualified.path;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.persistence.ElementCollection;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name="qual_path_Division")
|
||||||
|
public class Division {
|
||||||
|
@Id
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
@ElementCollection
|
||||||
|
private Map<Employee, String> employees = new HashMap<Employee, String>();
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Employee, String> getEmployees() {
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmployees(Map<Employee, String> employees) {
|
||||||
|
this.employees = employees;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.openjpa.persistence.jdbc.maps.qualified.path;
|
||||||
|
|
||||||
|
import javax.persistence.Embedded;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name="qual_path_Employee")
|
||||||
|
public class Employee {
|
||||||
|
@Id
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Embedded
|
||||||
|
private PersonalInfo personalInfo;
|
||||||
|
|
||||||
|
public PersonalInfo getPersonalInfo() {
|
||||||
|
return personalInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPersonalInfo(PersonalInfo personalInfo) {
|
||||||
|
this.personalInfo = personalInfo;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.openjpa.persistence.jdbc.maps.qualified.path;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.persistence.ElementCollection;
|
||||||
|
import javax.persistence.Embeddable;
|
||||||
|
|
||||||
|
@Embeddable
|
||||||
|
public class PersonalInfo {
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
@ElementCollection
|
||||||
|
private Set<Phone> phones = new HashSet<Phone>();
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Phone> getPhones() {
|
||||||
|
return phones;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPhones(Set<Phone> phones) {
|
||||||
|
this.phones = phones;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addPhone(Phone phone) {
|
||||||
|
phones.add(phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.openjpa.persistence.jdbc.maps.qualified.path;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name="qual_path_Phone")
|
||||||
|
public class Phone {
|
||||||
|
public final static String HOME= "home";
|
||||||
|
public final static String OFFICE = "office";
|
||||||
|
public final static String MOBILE = "mobile";
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
private int number;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Phone(int id, String type, int number) {
|
||||||
|
setId(id);
|
||||||
|
setType(type);
|
||||||
|
setNumber(number);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumber() {
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumber(int number) {
|
||||||
|
this.number = number;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,145 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.openjpa.persistence.jdbc.maps.qualified.path;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.persistence.Query;
|
||||||
|
|
||||||
|
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||||
|
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test queries containing qualified paths of the form:
|
||||||
|
* <pre>
|
||||||
|
* general_identification_variable.{single_valued_object_field.}*single_valued_object_field
|
||||||
|
* or
|
||||||
|
* general_identification_variable.{single_valued_object_field.}*collection_valued_field
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
public class TestQualifiedPath extends SQLListenerTestCase {
|
||||||
|
private int numDivisions = 2;
|
||||||
|
private int numEmployeesPerDivision = 3;
|
||||||
|
private int numMobilePhonesPerEmployee = 2;
|
||||||
|
|
||||||
|
private int divisionId = 0;
|
||||||
|
private int employeeId = 0;
|
||||||
|
private int nameCount = 0;
|
||||||
|
private int phoneId = 0;
|
||||||
|
private int phoneNumber = 1234567890;
|
||||||
|
|
||||||
|
OpenJPAEntityManager em;
|
||||||
|
|
||||||
|
public void setUp() {
|
||||||
|
super.setUp(CLEAR_TABLES,
|
||||||
|
Division.class, Employee.class, Phone.class, PersonalInfo.class);
|
||||||
|
assertNotNull(emf);
|
||||||
|
em = emf.createEntityManager();
|
||||||
|
assertNotNull(em);
|
||||||
|
createObj();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testQueries() {
|
||||||
|
em.clear();
|
||||||
|
String query = "select p " +
|
||||||
|
" from Division d, in(d.employees) e, in(KEY(e).personalInfo.phones) p";
|
||||||
|
Query q = em.createQuery(query);
|
||||||
|
List<?> rs = q.getResultList();
|
||||||
|
assertEquals(numDivisions*numEmployeesPerDivision*(2 + numMobilePhonesPerEmployee), rs.size());
|
||||||
|
|
||||||
|
em.clear();
|
||||||
|
query = "select KEY(e) " +
|
||||||
|
"from Division d, in(d.employees) e " +
|
||||||
|
"where KEY(e).personalInfo.lastName = 'lName2'";
|
||||||
|
q = em.createQuery(query);
|
||||||
|
rs = q.getResultList();
|
||||||
|
assertEquals(1, rs.size());
|
||||||
|
Employee employee = (Employee)rs.get(0);
|
||||||
|
assertEquals("lName2", employee.getPersonalInfo().getLastName());
|
||||||
|
|
||||||
|
em.clear();
|
||||||
|
query = "select KEY(e) " +
|
||||||
|
"from Division d, in(d.employees) e " +
|
||||||
|
"order by KEY(e).personalInfo.lastName";
|
||||||
|
q = em.createQuery(query);
|
||||||
|
rs = q.getResultList();
|
||||||
|
assertEquals(numDivisions * numEmployeesPerDivision, rs.size());
|
||||||
|
employee = (Employee)rs.get(0);
|
||||||
|
assertTrue(employee.getPersonalInfo().getLastName().equals("lName1"));
|
||||||
|
employee = (Employee)rs.get(1);
|
||||||
|
assertTrue(employee.getPersonalInfo().getLastName().equals("lName2"));
|
||||||
|
|
||||||
|
em.clear();
|
||||||
|
query = "select KEY(e).personalInfo.lastName " +
|
||||||
|
"from Division d, in (d.employees) e " +
|
||||||
|
"group by KEY(e).personalInfo.lastName " +
|
||||||
|
"having KEY(e).personalInfo.lastName = 'lName3'";
|
||||||
|
q = em.createQuery(query);
|
||||||
|
rs = q.getResultList();
|
||||||
|
assertEquals(1, rs.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createObj() {
|
||||||
|
em.getTransaction().begin();
|
||||||
|
for (int i = 0; i < numDivisions; i++) {
|
||||||
|
createDivision(divisionId++);
|
||||||
|
}
|
||||||
|
em.flush();
|
||||||
|
em.getTransaction().commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createDivision(int id) {
|
||||||
|
Division division = new Division();
|
||||||
|
division.setId(id);
|
||||||
|
Map<Employee, String> employees = new HashMap<Employee, String>();
|
||||||
|
for (int i = 0; i < numEmployeesPerDivision; i++) {
|
||||||
|
Employee employee = createEmployee(employeeId++);
|
||||||
|
employees.put(employee, employee.getPersonalInfo().getLastName());
|
||||||
|
}
|
||||||
|
division.setEmployees(employees);
|
||||||
|
em.persist(division);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Employee createEmployee(int id) {
|
||||||
|
Employee employee = new Employee();
|
||||||
|
employee.setId(id);
|
||||||
|
|
||||||
|
PersonalInfo personalInfo = new PersonalInfo();
|
||||||
|
personalInfo.setFirstName("fName" + nameCount++);
|
||||||
|
personalInfo.setLastName("lName" + nameCount);
|
||||||
|
|
||||||
|
Phone homePhone = new Phone(phoneId++, Phone.HOME, phoneNumber++);
|
||||||
|
personalInfo.addPhone(homePhone);
|
||||||
|
Phone officePhone = new Phone(phoneId++, Phone.OFFICE, phoneNumber++);
|
||||||
|
personalInfo.addPhone(officePhone);
|
||||||
|
for (int i = 0; i < numMobilePhonesPerEmployee; i++) {
|
||||||
|
Phone mobilePhone = new Phone(phoneId++, Phone.MOBILE, phoneNumber++);
|
||||||
|
personalInfo.addPhone(mobilePhone);
|
||||||
|
}
|
||||||
|
|
||||||
|
employee.setPersonalInfo(personalInfo);
|
||||||
|
|
||||||
|
em.persist(employee);
|
||||||
|
|
||||||
|
return employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue