openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r750469 - in /openjpa/trunk/openjpa-persistence-jdbc/src: main/java/org/apache/openjpa/persistence/jdbc/ test/java/org/apache/openjpa/persistence/embed/attrOverrides/
Date Thu, 05 Mar 2009 14:51:54 GMT
Author: faywang
Date: Thu Mar  5 14:51:53 2009
New Revision: 750469

URL: http://svn.apache.org/viewvc?rev=750469&view=rev
Log:
OPENJPA-937: AssociationOverrides support when applied to 
MappedSuperclass.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/AbstractEmployee.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PartTimeEmployee.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Project.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAssocOverrides.java
Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java
    openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PhoneNumber.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverridesXml.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java?rev=750469&r1=750468&r2=750469&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java
Thu Mar  5 14:51:53 2009
@@ -408,6 +408,7 @@
         JoinColumn[] scols;
         int unique;
         List<Column> jcols;
+        JoinTable joinTbl;
         for (AssociationOverride assoc : assocs) {
             if (StringUtils.isEmpty(assoc.name()))
                 throw new MetaDataException(_loc.get("no-override-name", cm));
@@ -416,16 +417,23 @@
                 sup = (FieldMapping) cm.addDefinedSuperclassField
                     (assoc.name(), Object.class, Object.class);
             scols = assoc.joinColumns();
-            if (scols == null || scols.length == 0)
-                continue;
+            joinTbl = assoc.joinTable();
+            if ((scols == null || scols.length == 0) && joinTbl == null)
+                //continue;
+                throw new MetaDataException(_loc.get("embed-override-name",
+                        sup, assoc.name()));
 
-            jcols = new ArrayList<Column>(scols.length);
-            unique = 0;
-            for (JoinColumn scol : scols) {
-                unique |= (scol.unique()) ? TRUE : FALSE;
-                jcols.add(newColumn(scol));
+            if (scols != null && scols.length > 0) {
+                jcols = new ArrayList<Column>(scols.length);
+                unique = 0;
+                for (JoinColumn scol : scols) {
+                    unique |= (scol.unique()) ? TRUE : FALSE;
+                    jcols.add(newColumn(scol));
+                }
+                setColumns(sup, sup.getValueInfo(), jcols, unique);
+            } else if (joinTbl != null) {
+                parseJoinTable(sup, joinTbl);
             }
-            setColumns(sup, sup.getValueInfo(), jcols, unique);
         }
     }
 

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java?rev=750469&r1=750468&r2=750469&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java
Thu Mar  5 14:51:53 2009
@@ -288,6 +288,7 @@
             case DISCRIM_VAL:
                 endDiscriminatorValue();
                 break;
+            case ASSOC_OVERRIDE:                
             case ATTR_OVERRIDE:
                 endAttributeOverride();
                 break;
@@ -632,11 +633,11 @@
     private void endAttributeOverride()
         throws SAXException {
         Object elem = currentElement();
-        FieldMapping fm;
+        FieldMapping fm = null;
         if (elem instanceof ClassMapping)
             fm = getAttributeOverride((ClassMapping) elem);
         else
-            fm = getAttributeOverride((FieldMapping) elem);
+            fm = getAttributeOverrideForEmbeddable((FieldMapping) elem);
         if (_cols != null) {
             fm.getValueInfo().setColumns(_cols);
             if (_colTable != null)
@@ -662,7 +663,7 @@
     /**
      * Return the proper override.
      */
-    private FieldMapping getAttributeOverride(FieldMapping fm) 
+    private FieldMapping getAttributeOverrideForEmbeddable(FieldMapping fm) 
     throws SAXException {
         return AnnotationPersistenceMappingParser.getEmbeddedFieldMapping(fm, 
             _override);
@@ -689,10 +690,16 @@
         String table = toTableName(attrs.getValue("schema"),
             attrs.getValue("name"));
         if (table != null) {
-            FieldMapping fm = (FieldMapping) currentElement();
-            if (_override != null) 
-                fm = getAttributeOverride(fm);
-            
+            Object elem = currentElement();
+            FieldMapping fm = null;
+            if (elem instanceof FieldMapping) {
+                fm = (FieldMapping) elem;
+                if (_override != null) 
+                    fm = getAttributeOverrideForEmbeddable(fm);
+            } else if (elem instanceof ClassMapping) {
+                ClassMapping cm = (ClassMapping) elem;
+                fm = getAttributeOverride(cm);
+            }
             fm.getMappingInfo().setTableName(table);
         }
         return true;
@@ -702,9 +709,16 @@
      * Set the join table information back.
      */
     private void endJoinTable() throws SAXException {
-        FieldMapping fm = (FieldMapping) currentElement();
-        if (_override != null)
-            fm = getAttributeOverride(fm);
+        Object elem = currentElement();
+        FieldMapping fm = null;
+        if (elem instanceof FieldMapping) {
+            fm = (FieldMapping) elem;
+            if (_override != null)
+                fm = getAttributeOverrideForEmbeddable(fm);
+        } else if (elem instanceof ClassMapping){
+            ClassMapping cm = (ClassMapping) elem;
+            fm = getAttributeOverride(cm);
+        }
 
         if (_joinCols != null)
             fm.getMappingInfo().setColumns(_joinCols);

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/AbstractEmployee.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/AbstractEmployee.java?rev=750469&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/AbstractEmployee.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/AbstractEmployee.java
Thu Mar  5 14:51:53 2009
@@ -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.embed.attrOverrides;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import javax.persistence.ManyToOne;
+import javax.persistence.MappedSuperclass;
+import javax.persistence.Version;
+
+@MappedSuperclass
+public abstract class AbstractEmployee {
+    @Id protected Integer id;
+    
+    @Version protected Integer version;
+    
+    @ManyToOne protected Address address;
+
+    @ManyToMany protected Collection<Project> projects = 
+    	new ArrayList<Project>();
+    
+    public Integer getId() {
+        return id;
+    }
+    
+    public void setId(Integer id) {
+        this.id = id;
+    }
+    
+    public Address getAddress() {
+        return address;
+    }
+    
+    public void setAddress(Address address) {
+        this.address = address;
+    }
+    
+    public Collection<Project> getProjects() {
+    	return projects;
+    }
+    
+    public void addProject(Project project) {
+    	projects.add(project);
+    }
+}

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java?rev=750469&r1=750468&r2=750469&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java
Thu Mar  5 14:51:53 2009
@@ -20,8 +20,10 @@
 
 import javax.persistence.*;
 
-@Embeddable 
+@Entity
+@Table(name="ADDR_ATTROVER")
 public class Address {
+    @Id int id;
     @Column(length = 20)
     protected String street;
     @Column(length = 20)
@@ -30,6 +32,14 @@
     protected String state;
     @Embedded protected Zipcode zipcode;
     
+    public int getId() {
+        return id;
+    }
+    
+    public void setId(int id) {
+        this.id = id;
+    }
+    
     public String getStreet() {
         return street;
     }

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PartTimeEmployee.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PartTimeEmployee.java?rev=750469&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PartTimeEmployee.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PartTimeEmployee.java
Thu Mar  5 14:51:53 2009
@@ -0,0 +1,57 @@
+/*
+ * 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.embed.attrOverrides;
+
+import javax.persistence.AssociationOverride;
+import javax.persistence.AssociationOverrides;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinTable;
+import javax.persistence.Table;
+
+@Entity
+@AssociationOverrides({
+	@AssociationOverride(
+		name="address",
+		joinColumns=@JoinColumn(name="ADDR_ID")),
+	
+	@AssociationOverride(
+		name="projects",
+		joinColumns={},
+    	joinTable=@JoinTable(
+       		name="PART_EMP_PROJECTS",
+       		joinColumns=@JoinColumn(name="PART_EMP"),
+       		inverseJoinColumns=@JoinColumn(name="PROJECT_ID")))
+})
+
+@Table(name="PART_EMP_ASSOC")
+public class PartTimeEmployee extends AbstractEmployee {
+
+	@Column(name="WAGE")
+	protected Float wage;
+
+    public Float getHourlyWage() {
+        return wage;
+    }
+    
+    public void setHourlyWage(Float wage) {
+        this.wage = wage;
+    }
+}

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PhoneNumber.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PhoneNumber.java?rev=750469&r1=750468&r2=750469&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PhoneNumber.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PhoneNumber.java
Thu Mar  5 14:51:53 2009
@@ -22,6 +22,7 @@
 import java.util.*;
 
 @Entity
+@Table(name="PHONE_ATTROVER")
 public class PhoneNumber {
     @Id 
     int number;

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Project.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Project.java?rev=750469&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Project.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Project.java
Thu Mar  5 14:51:53 2009
@@ -0,0 +1,46 @@
+/*
+ * 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.embed.attrOverrides;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name="PROJ_ATTROVER")
+public class Project {
+	@Id int id;
+	String description;
+	
+	public int getId() {
+		return id;
+	}
+	
+	public void setId(int id) {
+		this.id = id;
+	}
+	
+	public String getDescription() {
+		return description;
+	}
+	
+	public void setDescription(String description) {
+		this.description = description;
+	}
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAssocOverrides.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAssocOverrides.java?rev=750469&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAssocOverrides.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAssocOverrides.java
Thu Mar  5 14:51:53 2009
@@ -0,0 +1,279 @@
+/*
+ * 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.embed.attrOverrides;
+
+import java.util.Collection;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Query;
+
+import junit.framework.Assert;
+
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+public class TestAssocOverrides  extends SQLListenerTestCase { 
+    public int numEmployees = 2;
+    public int numPhoneNumbers = numEmployees + 1;
+    public int numEmployeesPerPhoneNumber = 2;
+    public int numPhoneNumbersPerEmployee = 2;
+    public int numJobsPerEmployee = 2;
+    public int empId = 1;
+    public int phoneId = 1;
+    public int addrId = 1;
+    public int pmId = 1;
+    public int peId = 1;
+    public int pId = 1;
+
+    public void setUp() throws Exception {
+        super.setUp(DROP_TABLES, Address.class, ContactInfo.class, 
+            EmergencyContactInfo.class, Employee.class, JobInfo.class,
+            PhoneNumber.class, ProgramManager.class, Zipcode.class,
+            AbstractEmployee.class, PartTimeEmployee.class, Project.class);
+    }
+
+    /**
+     * Test AssociationOverrides on field
+     */
+    public void testAssocOverride1() {
+        sql.clear();
+        createObj1();
+        findObj1();
+        queryObj1();
+        assertMappingOverrides("EMPPHONES");
+        assertMappingOverrides("EMP_ATTROVER");
+        assertMappingOverrides("EMP_ATTROVER_jobInfos");
+    }
+
+    /**
+     * Test AssociationOverrides on entity (MappedSuperclass)
+     */
+    public void testAssocOverride2() {
+        sql.clear();
+        createObj2();
+        findObj2();
+        queryObj2();
+        assertMappingOverrides("PART_EMP_ASSOC");
+        assertMappingOverrides("PART_EMP_PROJECTS");
+   }
+    
+	public void createObj1() {
+		EntityManager em = emf.createEntityManager();
+		EntityTransaction tran = em.getTransaction();
+		for (int i = 0; i < numEmployees; i++)
+		    createEmployee(em, empId++);
+		tran.begin();
+		em.flush();
+		tran.commit();
+        em.close();
+	}
+
+    public Employee createEmployee(EntityManager em, int id) {
+        Employee e = new Employee();
+        e.setEmpId(id);
+        ContactInfo contactInfo = new ContactInfo();
+        for (int i = 0; i < numPhoneNumbersPerEmployee; i++) { 
+            PhoneNumber phoneNumber = createPhoneNumber(em);
+            contactInfo.addPhoneNumber(phoneNumber);
+            e.setContactInfo(contactInfo);
+            phoneNumber.addEmployees(e);
+            em.persist(phoneNumber);
+        }
+        Address addr = new Address();
+        addr.setId(addrId++);
+        addr.setCity("city_" + addr.getId());
+        addr.setState("state_" + addr.getId());
+        addr.setStreet("street_" + addr.getId());
+        Zipcode zip = new Zipcode();
+        zip.setZip("zip_" + addr.getId());
+        zip.setPlusFour("+4_" + addr.getId());
+        addr.setZipcode(zip);
+        em.persist(addr);
+        contactInfo.setAddress(addr);
+                
+        EmergencyContactInfo ecInfo = new EmergencyContactInfo();
+        ecInfo.setFName("fName_" + id);
+        ecInfo.setLName("lName_" + id);
+        Address eaddr = new Address();
+        eaddr.setId(addrId++);
+        eaddr.setCity("city_" + eaddr.getId());
+        eaddr.setState("state_" + eaddr.getId());
+        eaddr.setStreet("street_" + eaddr.getId());
+        Zipcode ezip = new Zipcode();
+        ezip.setZip("zip_" + eaddr.getId());
+        ezip.setPlusFour("+4_" + eaddr.getId());
+        eaddr.setZipcode(ezip);
+        ecInfo.setAddress(eaddr);
+        contactInfo.setEmergencyContactInfo(ecInfo);
+        PhoneNumber phoneNumber = createPhoneNumber(em);
+        ecInfo.setPhoneNumber(phoneNumber);
+        em.persist(eaddr);
+        
+        for (int i = 0; i < numJobsPerEmployee; i++) {
+        	JobInfo job = new JobInfo();
+        	job.setJobDescription("job_" + id + "_" + i);
+        	ProgramManager pm = new ProgramManager();
+        	pm.setId(pmId++);
+        	pm.addManage(e);
+        	em.persist(pm);
+        	job.setProgramManager(pm);
+        	e.addJobInfo(job);
+        }
+        em.persist(e);
+        return e;
+    }
+	
+    public PhoneNumber createPhoneNumber(EntityManager em) {
+    	PhoneNumber p = new PhoneNumber();
+    	p.setNumber(phoneId++);
+    	em.persist(p);
+    	return p;
+    }    
+    
+	public void findObj1() {
+        EntityManager em = emf.createEntityManager();
+ 	    Employee e = em.find(Employee.class, 1);
+	    assertEmployee(e);
+	    em.close();
+	}
+	
+	public void queryObj1() {
+	    EntityManager em = emf.createEntityManager();
+	    EntityTransaction tran = em.getTransaction();
+	    tran.begin();
+	    Query q = em.createQuery("select e from Employee e");
+	    List<Employee> es = q.getResultList();
+	    for (Employee e : es){
+	        assertEmployee(e);
+	    }
+	    tran.commit();
+	    em.close();
+	}
+
+    public void assertEmployee(Employee e) {
+        int id = e.getEmpId();
+        ContactInfo c = e.getContactInfo();
+        List<PhoneNumber> phones = c.getPhoneNumbers();
+        for (PhoneNumber p : phones) {
+            assertPhoneNumber(p, e.getEmpId());
+        }
+    }
+	
+    public void assertPhoneNumber(PhoneNumber p, int empId) {
+        int number = p.getNumber();
+        Collection<Employee> es = p.getEmployees();
+        for (Employee e: es) {
+            assertEquals(empId, e.getEmpId());
+        }
+    }
+
+    public void createObj2() {
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+        for (int i = 0; i < numEmployees; i++)
+            createPartTimeEmployee(em, peId++);
+        tran.begin();
+        em.flush();
+        tran.commit();
+        em.close();
+    }
+
+    public PartTimeEmployee createPartTimeEmployee(EntityManager em, int id) {
+        PartTimeEmployee p = new PartTimeEmployee();
+        p.setId(id);
+        Address addr = new Address();
+        addr.setId(id);
+        addr.setCity("city_" + id);
+        addr.setState("state_" + id);
+        addr.setStreet("street_" + id);
+        p.setAddress(addr);
+        p.setHourlyWage((float)100);
+        Project project = new Project();
+        project.setId(pId++);
+        project.setDescription("p_" + project.getId());
+        p.addProject(project);
+        em.persist(addr);
+        em.persist(p);
+        em.persist(project);
+        return p;
+    }
+
+    public void findObj2() {
+        EntityManager em = emf.createEntityManager();
+        PartTimeEmployee p = em.find(PartTimeEmployee.class, 1);
+        Assert.assertEquals(p.getHourlyWage(), (float)100);
+    }
+
+    public void queryObj2() {
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+        tran.begin();
+        String firstName = "f_1";
+        String jpql = "select p from PartTimeEmployee p";
+        Query q = em.createQuery(jpql);
+        List<PartTimeEmployee> ps = q.getResultList();
+        Assert.assertEquals(ps.size(), numEmployees);
+        tran.commit();
+        em.close();
+    }
+    
+    public void assertMappingOverrides(String tableName) {
+        boolean found = false;
+        for (String sqlStr : sql) {
+            String key = "CREATE TABLE " + tableName + " ";
+            if (sqlStr.indexOf(key) != -1) {
+                if (tableName.equals("EMPPHONES")) {
+                    if (sqlStr.indexOf("EMP") == -1 ||
+                        sqlStr.indexOf("PHONE") == -1)
+                        fail();
+                    found = true;
+                } else if (tableName.equals("EMP_ATTROVER")) {
+                    if (sqlStr.indexOf("EMP_ADDR") == -1 ||
+                        sqlStr.indexOf("EMERGENCY_FNAME") == -1 ||
+                        sqlStr.indexOf("EMERGENCY_LNAME") == -1 ||
+                        sqlStr.indexOf("EMERGENCY_ADDR") == -1 ||
+                        sqlStr.indexOf("EMERGENCY_PHONE") == -1)
+                        fail();
+                    found = true;
+                } else if (tableName.equals("EMP_ATTROVER_jobInfos")) {
+                    if (sqlStr.indexOf("JOB_KEY") == -1 ||
+                        sqlStr.indexOf("JOB_DESC") == -1 ||
+                        sqlStr.indexOf("PROGRAM_MGR") == -1)
+                        fail();
+                    found = true;
+                } else if (tableName.equals("PART_EMP_ASSOC")) {
+                    if (sqlStr.indexOf("ADDR_ID") == -1)
+                        fail();
+                    found = true;
+                } else if (tableName.equals("PART_EMP_PROJECTS")) {
+                    if (sqlStr.indexOf("PART_EMP") == -1 ||
+                        sqlStr.indexOf("PROJECT_ID") == -1)
+                        fail();
+                    found = true;
+                }
+                break;
+            } 
+        }
+        if (!found) {
+            fail();
+        }
+    }
+
+}
\ No newline at end of file

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java?rev=750469&r1=750468&r2=750469&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java
Thu Mar  5 14:51:53 2009
@@ -41,6 +41,7 @@
 
     /**
      * This is spec 10.1.4 Example 2
+     * Test AttributeOverride on embeddable fields
      */
     public void testAttrOverride1() {
         sql.clear();
@@ -52,13 +53,14 @@
 
     /**
      * This is spec 10.1.4. Example 3
+     * Test AttributeOverrides on embeddable Map field
      */
     public void testAttrOverride2() {
         sql.clear();
         createObj2();
         findObj2();
         queryObj2();
-        assertAttrOverrides("PROPREC_ATTROVER_PARCELS");
+        assertAttrOverrides("PROPREC_ATTROVER_parcels");
     }
     
     public void createObj1() {
@@ -188,21 +190,26 @@
     }
     
     public void assertAttrOverrides(String tableName) {
+        boolean found = false;
         for (String sqlStr : sql) {
-            if (sqlStr.toUpperCase().indexOf("CREATE TABLE " + tableName) != -1) {
+            if (sqlStr.indexOf("CREATE TABLE " + tableName + " ") != -1) {
                 if (tableName.equals("CUS_ATTROVER")) {
+                    found = true;
                     if (sqlStr.indexOf("ADDR_STATE") == -1 ||
                         sqlStr.indexOf("ADDR_ZIP") == -1 ||
                         sqlStr.indexOf("ADDR_PLUSFOUR") == -1 )
                         fail();
-                } else if (tableName.equals("PROPREC_ATTROVER")) {
+                } else if (tableName.equals("PROPREC_ATTROVER_parcels")) {
+                    found = true;
                     if (sqlStr.indexOf("STREET_NAME") == -1 ||
                         sqlStr.indexOf("SQUARE_FEET") == -1 ||
                         sqlStr.indexOf("ASSESSMENT") == -1 )
                         fail();
                 }
+                break;
             }
         }
+        if (!found)
+            fail();
     }
-
 }
\ No newline at end of file

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverridesXml.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverridesXml.java?rev=750469&r1=750468&r2=750469&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverridesXml.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverridesXml.java
Thu Mar  5 14:51:53 2009
@@ -27,16 +27,16 @@
 
 import junit.framework.Assert;
 
-import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
 
-public class TestAttrOverridesXml extends SingleEMFTestCase {
+public class TestAttrOverridesXml extends SQLListenerTestCase {
    
     public int numPersons = 4;
     public List<String> namedQueries = new ArrayList<String>();
     public int eId = 1;
     
     public void setUp() {
-        setUp(CLEAR_TABLES);
+        setUp(DROP_TABLES);
     }
     
     @Override
@@ -45,9 +45,11 @@
     }
     
     public void testAttrOverride1() {
+        sql.clear();
     	createObj1();
     	findObj1();
     	queryObj1();
+        assertAttrOverrides("CustomerXml1");
     }
     
     public void createObj1() {
@@ -95,4 +97,20 @@
         tran.commit();
         em.close();
     }
+
+    public void assertAttrOverrides(String tableName) {
+        boolean found = false;
+        for (String sqlStr : sql) {
+            if (sqlStr.indexOf("CREATE TABLE " + tableName) != -1) {
+                if (tableName.equals("CustomerXml1")) {
+                    found = true;
+                    if (sqlStr.indexOf("ADDR_STATE") == -1 ||
+                        sqlStr.indexOf("ADDR_ZIP") == -1)
+                        fail();
+                } 
+            }
+        }
+        if (!found)
+            fail();
+    }
 }



Mime
View raw message