diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/Division.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/Division.java new file mode 100644 index 000000000..b83eef2fd --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/Division.java @@ -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 employees = new HashMap(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Map getEmployees() { + return employees; + } + + public void setEmployees(Map employees) { + this.employees = employees; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/Employee.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/Employee.java new file mode 100644 index 000000000..bebfeb7d9 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/Employee.java @@ -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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/PersonalInfo.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/PersonalInfo.java new file mode 100644 index 000000000..5806f2732 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/PersonalInfo.java @@ -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 phones = new HashSet(); + + 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 getPhones() { + return phones; + } + + public void setPhones(Set phones) { + this.phones = phones; + } + + public void addPhone(Phone phone) { + phones.add(phone); + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/Phone.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/Phone.java new file mode 100644 index 000000000..9ef723d3e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/Phone.java @@ -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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/TestQualifiedPath.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/TestQualifiedPath.java new file mode 100644 index 000000000..0311d614f --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/maps/qualified/path/TestQualifiedPath.java @@ -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: + *
+ * general_identification_variable.{single_valued_object_field.}*single_valued_object_field 
+ *    or
+ * general_identification_variable.{single_valued_object_field.}*collection_valued_field 
+ * 
+ */ +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 employees = new HashMap(); + 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; + } + +}