openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r746630 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/e...
Date Sun, 22 Feb 2009 04:11:32 GMT
Author: faywang
Date: Sun Feb 22 04:11:32 2009
New Revision: 746630

URL: http://svn.apache.org/viewvc?rev=746630&view=rev
Log:
OPENJPA-930: AttributeOverride support

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/
    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/Customer.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyInfo.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyOwner.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyRecord.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/Zipcode.java
Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java
    openjpa/trunk/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java?rev=746630&r1=746629&r2=746630&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java
Sun Feb 22 04:11:32 2009
@@ -268,7 +268,7 @@
             if (fmds[i].getValue().getEmbeddedMetaData() == null) {
                 getIdColumns(fmds[i], cols);
             } else {
-                getEmbeddedIdCols(fmd, cols);
+                getEmbeddedIdCols(fmds[i], cols);
             }
         }
     }

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=746630&r1=746629&r2=746630&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
Sun Feb 22 04:11:32 2009
@@ -25,6 +25,7 @@
 import java.sql.Types;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -71,6 +72,7 @@
 import org.apache.openjpa.jdbc.meta.QueryResultMapping;
 import org.apache.openjpa.jdbc.meta.SequenceMapping;
 import org.apache.openjpa.jdbc.meta.ValueMapping;
+import org.apache.openjpa.jdbc.meta.ValueMappingImpl;
 import org.apache.openjpa.jdbc.meta.ValueMappingInfo;
 import org.apache.openjpa.jdbc.meta.strats.EnumValueHandler;
 import org.apache.openjpa.jdbc.meta.strats.FlatClassStrategy;
@@ -87,6 +89,7 @@
 import org.apache.openjpa.meta.FieldMetaData;
 import org.apache.openjpa.meta.JavaTypes;
 import org.apache.openjpa.meta.MetaDataContext;
+import org.apache.openjpa.meta.ValueMetaData;
 import org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser;
 import static org.apache.openjpa.persistence.jdbc.MappingTag.*;
 import org.apache.openjpa.util.InternalException;
@@ -1248,23 +1251,85 @@
      */
     private void parseAttributeOverrides(FieldMapping fm,
         AttributeOverride... attrs) {
-        ClassMapping embed = fm.getEmbeddedMapping();
-        if (embed == null) {
-            embed = fm.getElementMapping().getEmbeddedMapping();
-            if (embed == null)
-                throw new MetaDataException(_loc.get("not-embedded", fm));
-        }
-        FieldMapping efm;
         for (AttributeOverride attr : attrs) {
-            efm = embed.getFieldMapping(attr.name());
-            if (efm == null)
-                throw new MetaDataException(_loc.get("embed-override-name",
-                    fm, attr.name()));
+            String attrName = attr.name();
+            FieldMapping efm = getEmbeddedFieldMapping(fm, attrName);
             if (attr.column() != null)
                 parseColumns(efm, attr.column());
         }
     }
+    
+    private FieldMapping getEmbeddedFieldMapping(FieldMapping fm, String attrName) {
+        ClassMapping embed = null;
+        boolean isKey = false;
+        boolean isValue = false;
+        if (attrName != null && attrName.startsWith("key."))
+            isKey = true;
+        else if (attrName != null && attrName.startsWith("value."))
+            isValue = true;
+        if (isKey || isValue)
+            attrName = attrName.substring(attrName.indexOf(".")+1);
+            
+        int typeCode = fm.getValue().getDeclaredTypeCode();
+        switch (typeCode) {
+            case JavaTypes.COLLECTION : // a collection of embeddables
+                if (isKey || isValue)
+                    throw new MetaDataException(_loc.get("embed-override-name",
+                        fm, attrName));
+                embed = fm.getElementMapping().getEmbeddedMapping();
+                break;
+            case JavaTypes.MAP: // a map
+                if (!isKey && !isValue)
+                    throw new MetaDataException(_loc.get("embed-override-name",
+                        fm, attrName));
+                if (isKey) 
+                    embed = getEmbeddedMapping(fm.getKeyMapping());
+                else if (isValue)     
+                    embed = getEmbeddedMapping(fm.getElementMapping());
+                break;
+            default: // an embeddable
+                if (isKey || isValue)
+                    throw new MetaDataException(_loc.get("embed-override-name",
+                        fm, attrName));
+                embed = getEmbeddedMapping(fm.getValueMapping());
+                break;
+        }
+        
+        if (embed == null) 
+            throw new MetaDataException(_loc.get("not-embedded", fm));
+        return getAttributeOverrideField(attrName, fm, embed);
+    }
+    
+    private ClassMapping getEmbeddedMapping(ValueMapping val) {
+        ClassMapping embed = val.getEmbeddedMapping();
+        if (embed != null) 
+            return embed;
+        
+        val.addEmbeddedMetaData();
+        return val.getEmbeddedMapping();
 
+    }
+    
+    public FieldMapping getAttributeOverrideField(String attrName, FieldMapping fm, ClassMapping
embed) {
+        FieldMapping efm;
+        int idxOfDot = attrName.indexOf("."); 
+        if (idxOfDot == -1) {
+            efm = embed.getFieldMapping(attrName);
+            if (efm == null)
+                throw new MetaDataException(_loc.get("embed-override-name",
+                    fm, attrName));
+            return efm;
+        } 
+        String attrName1 = attrName.substring(0, idxOfDot);
+        String attrName2 = attrName.substring(idxOfDot+1);
+        efm = embed.getFieldMapping(attrName1);
+        if (efm == null)
+            throw new MetaDataException(_loc.get("embed-override-name",
+                fm, attrName1));
+        ClassMapping embed1 = getEmbeddedMapping(efm.getValueMapping());
+        return getAttributeOverrideField(attrName2, efm, embed1);
+    }
+    
     /**
      * Parse @Enumerated.
      */

Added: 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=746630&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java
Sun Feb 22 04:11:32 2009
@@ -0,0 +1,85 @@
+/*
+ * 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.*;
+
+@Embeddable 
+public class Address {
+    @Column(length = 20)
+    protected String street;
+    @Column(length = 20)
+    protected String city;
+    @Column(length = 20)
+    protected String state;
+    @Embedded protected Zipcode zipcode;
+    
+    public String getStreet() {
+        return street;
+    }
+    
+    public void setStreet(String street) {
+        this.street = street;
+    }
+    
+    public String getCity() {
+        return city;
+    }
+    
+    public void setCity(String city) {
+        this.city = city;
+    }
+    
+    public String getState() {
+        return state;
+    }
+    
+    public void setState(String state) {
+        this.state = state;
+    }
+    
+    public Zipcode getZipcode() {
+    	return zipcode;
+    }
+    
+    public void setZipcode(Zipcode zipcode) {
+    	this.zipcode = zipcode;
+    }
+    
+	public boolean equals(Object o) {
+		if (o == this) return true;
+		if (!(o instanceof Address)) return false;
+		Address a = (Address) o;
+		if (!city.equals(a.city)) return false;
+		if (!state.equals(a.state)) return false;
+		if (!street.equals(a.street)) return false;
+		if (zipcode != null && !zipcode.equals(a.zipcode)) return false;
+		return true;
+	}
+	
+	public int hashCode() {
+		int ret = 0;
+		ret = ret + 31 * city.hashCode();
+		ret = ret + 31 * state.hashCode();
+		ret = ret + 31 * street.hashCode();
+		if (zipcode != null)
+		    ret = ret + 31 * zipcode.hashCode();
+		return ret;
+	}
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Customer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Customer.java?rev=746630&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Customer.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Customer.java
Sun Feb 22 04:11:32 2009
@@ -0,0 +1,64 @@
+/*
+ * 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
+@Table(name="CUS_ATTROVER")
+public class Customer {
+    @Id protected Integer id;
+    
+    protected String name;
+    
+    @AttributeOverrides({
+        @AttributeOverride(name="state", 
+        		column=@Column(name="ADDR_STATE")),
+        @AttributeOverride(name="zipcode.zip", 
+        		column=@Column(name="ADDR_ZIP")),
+        @AttributeOverride(name="zipcode.plusFour", 
+                column=@Column(name="ADDR_PLUSFOUR"))})
+    @Embedded
+    protected Address address;
+    
+    public Integer getId() {
+    	return id;
+    }
+    
+    public void setId(Integer id) {
+    	this.id = id;
+    }
+    
+    public String getName() {
+    	return name;
+    }
+    
+    public void setName(String name) {
+    	this.name = name;
+    }
+    
+    public Address getAddress() {
+    	return address;
+    }
+    
+    public void setAddress(Address address) {
+    	this.address = address;
+    }
+}
+

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyInfo.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyInfo.java?rev=746630&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyInfo.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyInfo.java
Sun Feb 22 04:11:32 2009
@@ -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.embed.attrOverrides;
+
+import java.math.BigDecimal;
+import javax.persistence.*;
+
+@Embeddable 
+public class PropertyInfo {
+    protected Integer parcelNumber;
+    protected Integer size;
+    protected BigDecimal tax;
+    
+    public Integer getParcelNumber() {
+    	return parcelNumber;
+    }
+    
+    public void setParcelNumber(Integer p) {
+    	parcelNumber = p;
+    }
+    
+    public Integer getSize() {
+    	return size;
+    }
+    
+    public void setSize(Integer size) {
+    	this.size = size;
+    }
+    
+    public BigDecimal getTax() {
+    	return tax;
+    }
+    
+    public void setTax(BigDecimal tax) {
+    	this.tax = tax;
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyOwner.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyOwner.java?rev=746630&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyOwner.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyOwner.java
Sun Feb 22 04:11:32 2009
@@ -0,0 +1,60 @@
+/*
+ * 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.*;
+
+@Embeddable
+public class PropertyOwner {
+	@Embedded
+	protected Address addr;
+	@Column(length = 10)
+	protected String ssn;
+	
+	public Address getAddress() {
+		return addr;
+	}
+	
+	public void setAddress(Address addr) {
+		this.addr = addr;
+	}
+	
+	public String getSsn() {
+		return ssn;
+	}
+
+	public void setSsn(String ssn) {
+		this.ssn = ssn;
+	}
+	
+	public boolean equals(Object o) {
+		if (o == this) return true;
+		if (!(o instanceof PropertyOwner)) return false;
+		PropertyOwner p = (PropertyOwner) o;
+		if (!ssn.equals(p.ssn)) return false;
+		if (!addr.equals(p.addr)) return false;
+		return true;
+	}
+	
+	public int hashCode() {
+		int ret = 0;
+		ret = ret + 31 * ssn.hashCode();
+		ret = ret + 31 * addr.hashCode();
+		return ret;
+	}
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyRecord.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyRecord.java?rev=746630&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyRecord.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/PropertyRecord.java
Sun Feb 22 04:11:32 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.HashMap;
+import java.util.Map;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name="PROPREC_ATTROVER")
+public class PropertyRecord {
+    @EmbeddedId PropertyOwner owner;
+    
+    @AttributeOverrides({
+        @AttributeOverride(name="key.street", column=@Column(name="STREET_NAME")),
+        @AttributeOverride(name="value.size", column=@Column(name="SQUARE_FEET")),
+        @AttributeOverride(name="value.tax", column=@Column(name="ASSESSMENT"))
+    })
+    @ElementCollection
+    Map<Address, PropertyInfo> parcels = new HashMap<Address, PropertyInfo>();
+    
+    @Column(length = 10)
+    String description;
+    
+    public String getDesc() {
+        return description;
+    }
+    
+    public void setDesc(String desc) {
+        this.description = desc;
+    }
+    
+    public PropertyOwner getOwner() {
+    	return owner;
+    }
+    
+    public void setOwner(PropertyOwner owner) {
+    	this.owner = owner;
+    }
+    
+    public Map<Address, PropertyInfo> getParcels() {
+    	return parcels;
+    }
+    
+    public void addParcel(Address addr, PropertyInfo p) {
+    	parcels.put(addr, p);
+    }
+}

Added: 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=746630&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java
Sun Feb 22 04:11:32 2009
@@ -0,0 +1,208 @@
+/*
+ * 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.math.BigDecimal;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Query;
+
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+public class TestAttrOverrides  extends SQLListenerTestCase { 
+    public int numPersons = 4;
+    public int numPropertiesPerPersons = 4;
+    public int eId = 1;
+    public int pId = 1;
+
+    public void setUp() throws Exception {
+        super.setUp(DROP_TABLES, Address.class, Customer.class, 
+            PropertyInfo.class, PropertyOwner.class, PropertyRecord.class,
+            Zipcode.class);
+    }
+
+    /**
+     * This is spec 10.1.4 Example 2
+     */
+    public void testMappedById1() {
+        sql.clear();
+        createObj1();
+        findObj1();
+        queryObj1();
+        assertAttrOverrides("CUS_ATTROVER");
+    }
+
+    /**
+     * This is spec 10.1.4. Example 3
+     */
+    public void testMappedById2() {
+        sql.clear();
+        createObj2();
+        findObj2();
+        queryObj2();
+        assertAttrOverrides("PROPREC_ATTROVER_PARCELS");
+    }
+    
+    public void createObj1() {
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+        for (int i = 0; i < numPersons; i++)
+            createCustomer(em, eId++);
+        tran.begin();
+        em.flush();
+        tran.commit();
+        em.close();
+    }
+
+    public Customer createCustomer(EntityManager em, int id) {
+        Customer p = new Customer();
+        p.setId(id);
+        Address addr = new Address();
+        addr.setCity("city_" + id);
+        addr.setState("state_" + id);
+        addr.setStreet("street_" + id);
+        p.setAddress(addr);
+        p.setName("name_" + id);
+        em.persist(p);
+        return p;
+    }
+
+    public void findObj1() {
+        EntityManager em = emf.createEntityManager();
+        Customer p = em.find(Customer.class, 1);
+        assertEquals(p.getId(), new Integer(1));
+        assertEquals(p.getAddress().getCity(), "city_1");
+        assertEquals(p.getAddress().getStreet(), "street_1");
+        assertEquals(p.getAddress().getState(), "state_1");
+        assertEquals(p.getName(), "name_1");
+    }
+
+    public void queryObj1() {
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+        tran.begin();
+        String jpql = "select p from Customer p";
+        Query q = em.createQuery(jpql);
+        List<Customer> ps = q.getResultList();
+        assertEquals(ps.size(), numPersons);
+        tran.commit();
+        em.close();
+    }
+
+    public void createObj2() {
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+        for (int i = 0; i < numPersons; i++)
+            createPropertyRecord(em, pId++);
+        tran.begin();
+        em.flush();
+        tran.commit();
+        em.close();
+    }
+
+    public PropertyRecord createPropertyRecord(EntityManager em, int id) {
+        PropertyRecord p = new PropertyRecord();
+        PropertyOwner owner = new PropertyOwner();
+        owner.setSsn("ssn_" + id);
+        Address addr = new Address();
+        addr.setCity("city_" + id);
+        addr.setState("state_" + id);
+        addr.setStreet("street_" + id);
+        Zipcode zipcode = new Zipcode();
+        zipcode.setZip("zip_" + id);
+        zipcode.setPlusFour("+4_" + id);
+        addr.setZipcode(zipcode);
+        owner.setAddress(addr);
+        p.setOwner(owner);
+        for (int i = 0; i < numPropertiesPerPersons; i++) {
+            PropertyInfo info = new PropertyInfo();
+            info.setParcelNumber(id*10 + i);
+            info.setSize(id*10 + i);
+            info.setTax(new BigDecimal(id*10 + i));
+            Address paddr = new Address();
+            paddr.setCity("pcity_" + id + "_" + i);
+            paddr.setState("pstate_" + id + "_" + i);
+            paddr.setStreet("pstreet_" + id + "_" + i);
+            Zipcode pzipcode = new Zipcode();
+            pzipcode.setZip("pzip_" + id + "_" + i);
+            pzipcode.setPlusFour("p+4_" + id + "_" + i);
+            paddr.setZipcode(zipcode);
+            p.addParcel(paddr, info);
+        }
+        em.persist(p);
+        return p;
+    }
+
+    public void findObj2() {
+        EntityManager em = emf.createEntityManager();
+        PropertyOwner owner = new PropertyOwner();
+        owner.setSsn("ssn_1");
+        Address addr = new Address();
+        addr.setCity("city_1");
+        addr.setState("state_1");
+        addr.setStreet("street_1");
+        Zipcode zipcode = new Zipcode();
+        zipcode.setZip("zip_1");
+        zipcode.setPlusFour("+4_1");
+        addr.setZipcode(zipcode);
+        owner.setAddress(addr);
+        PropertyRecord p = em.find(PropertyRecord.class, owner);
+        assertEquals(p.getOwner().getSsn(), "ssn_1");
+        assertEquals(p.getOwner().getAddress().getCity(), "city_1");
+        assertEquals(p.getOwner().getAddress().getStreet(), "street_1");
+        assertEquals(p.getOwner().getAddress().getState(), "state_1");
+        assertEquals(p.getOwner().getAddress().getZipcode().getZip(), "zip_1");
+        assertEquals(p.getOwner().getAddress().getZipcode().getPlusFour(), "+4_1");
+
+        assertEquals(p.getParcels().size(), numPropertiesPerPersons);
+    }
+
+    public void queryObj2() {
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+        tran.begin();
+        String jpql = "select p from PropertyRecord p";
+        Query q = em.createQuery(jpql);
+        List<PropertyRecord> ps = q.getResultList();
+        assertEquals(ps.size(), numPersons);
+        tran.commit();
+        em.close();
+    }
+    
+    public void assertAttrOverrides(String tableName) {
+        for (String sqlStr : sql) {
+            if (sqlStr.toUpperCase().indexOf("CREATE TABLE " + tableName) != -1) {
+                if (tableName.equals("CUS_ATTROVER")) {
+                    if (sqlStr.indexOf("ADDR_STATE") == -1 ||
+                        sqlStr.indexOf("ADDR_ZIP") == -1 ||
+                        sqlStr.indexOf("ADDR_PLUSFOUR") == -1 )
+                        fail();
+                } else if (tableName.equals("PROPREC_ATTROVER")) {
+                    if (sqlStr.indexOf("STREET_NAME") == -1 ||
+                        sqlStr.indexOf("SQUARE_FEET") == -1 ||
+                        sqlStr.indexOf("ASSESSMENT") == -1 )
+                        fail();
+                }
+            }
+        }
+    }
+
+}
\ No newline at end of file

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Zipcode.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Zipcode.java?rev=746630&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Zipcode.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Zipcode.java
Sun Feb 22 04:11:32 2009
@@ -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.embed.attrOverrides;
+ 
+
+import javax.persistence.*;
+
+@Embeddable
+public class Zipcode {
+    @Column(length = 5)
+	protected String zip;
+    @Column(length = 4)
+	protected String plusFour;
+	
+	public String getZip() {
+		return zip;
+	}
+	
+	public void setZip(String zip) {
+		this.zip = zip;
+	}
+	
+	public String getPlusFour() {
+		return plusFour;
+	}
+	
+	public void setPlusFour(String plusFour) {
+		this.plusFour = plusFour;
+	}
+	
+	public boolean equals(Object o) {
+		if (o == this) return true;
+		if (!(o instanceof Zipcode)) return false;
+		Zipcode z = (Zipcode) o;
+		if (!zip.equals(z.zip)) return false;
+		if (!plusFour.equals(z.plusFour)) return false;
+		return true;
+	}
+	
+	public int hashCode() {
+		int ret = 0;
+		ret = ret + 31 * zip.hashCode();
+		ret = ret + 31 * plusFour.hashCode();
+		return ret;
+	}
+}



Mime
View raw message