OPENJPA-871: derived identity test case using orm xml

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@826848 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-10-19 22:41:21 +00:00
parent 58ebc6d9f1
commit 22054669db
6 changed files with 412 additions and 0 deletions

View File

@ -0,0 +1,62 @@
/*
* 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.enhance.identity;
public class Dependent1Xml {
DependentId1Xml id;
Employee1Xml emp;
public Employee1Xml getEmp() {
return emp;
}
public void setEmp(Employee1Xml emp) {
this.emp = emp;
}
public DependentId1Xml getId() {
return id;
}
public void setId(DependentId1Xml id) {
this.id = id;
}
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof Dependent1Xml)) return false;
Dependent1Xml d0 = (Dependent1Xml)o;
DependentId1Xml id0 = d0.getId();
if (!id.equals(id0)) return false;
Employee1Xml e0 = d0.getEmp();
if (emp != null && !emp.equals(e0)) return false;
if (emp == null && e0 != null) return false;
return true;
}
public int hashCode() {
int ret = 0;
ret = ret * 31 + id.hashCode();
ret = ret * 31 + emp.hashCode();
return ret;
}
}

View File

@ -0,0 +1,65 @@
/*
* 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.enhance.identity;
public class DependentId1Xml {
String name;
long empPK;
public DependentId1Xml() {}
public DependentId1Xml(String name, long empPK) {
this.name = name;
this.empPK = empPK;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setEmpPK(long empPK) {
this.empPK = empPK;
}
public long getEmpPK() {
return empPK;
}
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof DependentId1Xml)) return false;
DependentId1Xml d = (DependentId1Xml)o;
if (empPK != d.getEmpPK()) return false;
if (name != null && !name.equals(d.getName())) return false;
if (name == null && d.getName() != null) return false;
return true;
}
public int hashCode() {
int ret = 0;
ret = (int) (ret * 31 + empPK);
ret = ret * 31 + name.hashCode();
return ret;
}
}

View File

@ -0,0 +1,86 @@
/*
* 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.enhance.identity;
import java.util.ArrayList;
import java.util.List;
public class Employee1Xml {
int empId;
String name;
List<Dependent1Xml> dependents = new ArrayList<Dependent1Xml>();
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Dependent1Xml> getDependents() {
return dependents;
}
public void setDependents(List<Dependent1Xml> dependents) {
this.dependents = dependents;
}
public void addDependent(Dependent1Xml d) {
dependents.add(d);
}
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof Employee1Xml)) return false;
Employee1Xml e = (Employee1Xml)o;
if (empId != e.getEmpId()) return false;
if (name != null && !name.equals(e.getName())) return false;
if (name == null && e.getName() != null) return false;
List<Dependent1Xml> ds0 = e.getDependents();
if (ds0 != null && ds0.size() != 0 && dependents == null) return false;
if (ds0 == null && dependents != null && dependents.size() != 0)
return false;
if (ds0 == null && dependents == null) return true;
if (ds0 != null && dependents != null) {
if (ds0.size() != dependents.size()) return false;
}
return true;
}
public int hashCode() {
int ret = 0;
ret = ret * 31 + empId;
ret = ret * 31 + name.hashCode();
if (dependents != null)
for (Dependent1Xml d : dependents)
ret = ret * 31 + d.id.hashCode();
return ret;
}
}

View File

@ -0,0 +1,130 @@
/*
* 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.enhance.identity;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestMapsIdXml extends SingleEMFTestCase {
public int numEmployees = 4;
public int numDependentsPerEmployee = 2;
public int numPersons = 4;
public Map<Integer, Employee1Xml> emps1xml = new HashMap<Integer, Employee1Xml>();
public Map<String, Dependent1Xml> deps1xml = new HashMap<String, Dependent1Xml>();
public int eId1 = 1;
public int dId1 = 1;
public void setUp() throws Exception {
super.setUp(CLEAR_TABLES);
}
@Override
protected String getPersistenceUnitName() {
return "mapsId-pu";
}
/**
* This is spec 2.4.1.2 Example 1, case(b)
*/
public void testMapsId1Xml() {
createObj1Xml();
findObj1Xml();
queryObj1Xml();
}
public void createObj1Xml() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++)
createEmployee1Xml(em, eId1++);
tran.begin();
em.flush();
tran.commit();
em.close();
}
public Employee1Xml createEmployee1Xml(EntityManager em, int id) {
Employee1Xml e = new Employee1Xml();
e.setEmpId(id);
e.setName("emp_" + id);
for (int i = 0; i < numDependentsPerEmployee; i++) {
Dependent1Xml d = createDependent1Xml(em, dId1++, e);
e.addDependent(d);
em.persist(d);
}
em.persist(e);
emps1xml.put(id, e);
return e;
}
public Dependent1Xml createDependent1Xml(EntityManager em, int id, Employee1Xml e) {
Dependent1Xml d = new Dependent1Xml();
DependentId1Xml did = new DependentId1Xml();
did.setName("dep_" + id);
d.setId(did);
d.setEmp(e);
deps1xml.put(did.getName(), d);
return d;
}
public void findObj1Xml() {
EntityManager em = emf.createEntityManager();
Employee1Xml e = em.find(Employee1Xml.class, 1);
List<Dependent1Xml> ds = e.getDependents();
assertEquals(numDependentsPerEmployee, ds.size());
Employee1Xml e0 = emps1xml.get(1);
assertEquals(e0, e);
}
public void queryObj1Xml() {
queryDependent1Xml();
}
public void queryDependent1Xml() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
String jpql = "select d from Dependent1Xml d where d.id.name = 'dep_1' "
+ "AND d.emp.name = 'emp_1'";
Query q = em.createQuery(jpql);
List<Dependent1Xml> ds = q.getResultList();
for (Dependent1Xml d : ds) {
assertDependent1Xml(d);
}
tran.commit();
em.close();
}
public void assertDependent1Xml(Dependent1Xml d) {
DependentId1Xml id = d.getId();
Dependent1Xml d0 = deps1xml.get(id.getName());
if (d0.id.empPK == 0)
d0.id.empPK = d0.emp.getEmpId();
assertEquals(d0, d);
}
}

View File

@ -52,6 +52,7 @@
<mapping-file>org/apache/openjpa/persistence/orm.xml</mapping-file>
<mapping-file>org/apache/openjpa/persistence/embed/embed-assoc-over-orm.xml</mapping-file>
<mapping-file>org/apache/openjpa/persistence/detach/detach-orm.xml</mapping-file>
<mapping-file>org/apache/openjpa/persistence/enhance/identity/mapsId-orm.xml</mapping-file>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)"/>
@ -255,5 +256,18 @@
<class>org.apache.openjpa.persistence.meta.MdrTestEntity</class>
</persistence-unit>
<persistence-unit name="mapsId-pu">
<mapping-file>org/apache/openjpa/persistence/enhance/identity/mapsId-orm.xml</mapping-file>
<class>org.apache.openjpa.persistence.enhance.identity.Employee1Xml</class>
<class>org.apache.openjpa.persistence.enhance.identity.Dependent1Xml</class>
<class>org.apache.openjpa.persistence.enhance.identity.DependentId1Xml</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)"/>
<property name="openjpa.DynamicEnhancementAgent"
value="true"/>
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<!-- AtestCatalog metadata in xml -->
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0">
<package>mapsIdXml</package>
<entity name="Employee1Xml"
class="org.apache.openjpa.persistence.enhance.identity.Employee1Xml"
access="FIELD">
<table name="EMP1XML_MBI" />
<attributes>
<id name="empId"></id>
<basic name="name"/>
<one-to-many name="dependents" mapped-by="emp"/>
</attributes>
</entity>
<entity name="Dependent1Xml"
class="org.apache.openjpa.persistence.enhance.identity.Dependent1Xml"
access="FIELD">
<attributes>
<embedded-id name="id"></embedded-id>
<many-to-one name="emp" maps-id="empPK"/>
</attributes>
</entity>
<embeddable class="org.apache.openjpa.persistence.enhance.identity.DependentId1Xml"
access="FIELD">
<attributes>
<basic name="name"></basic>
<basic name="empPK"></basic>
</attributes>
</embeddable>
</entity-mappings>