From 62c76e42eaae0fef7a25ed69595cc9d94638d5e0 Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Tue, 24 Feb 2009 00:39:48 +0000 Subject: [PATCH] OPENJPA-930: orm xml support for AttributeOverride git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@747239 13f79535-47bb-0310-9956-ffa450edef68 --- .../AnnotationPersistenceMappingParser.java | 6 +- .../jdbc/XMLPersistenceMappingParser.java | 18 +--- .../embed/attrOverrides/AddressXml.java | 50 ++++++++++ .../embed/attrOverrides/CustomerXml.java | 52 ++++++++++ .../attrOverrides/TestAttrOverridesXml.java | 98 +++++++++++++++++++ .../embed/attrOverrides/ZipcodeXml.java | 40 ++++++++ .../openjpa/persistence/embed/embed-orm.xml | 38 +++++++ 7 files changed, 286 insertions(+), 16 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/AddressXml.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/CustomerXml.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverridesXml.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/ZipcodeXml.java diff --git a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java index 74d27bcba..6c6672f15 100644 --- a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java +++ b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java @@ -1259,7 +1259,7 @@ public class AnnotationPersistenceMappingParser } } - private FieldMapping getEmbeddedFieldMapping(FieldMapping fm, String attrName) { + public static FieldMapping getEmbeddedFieldMapping(FieldMapping fm, String attrName) { ClassMapping embed = null; boolean isKey = false; boolean isValue = false; @@ -1300,7 +1300,7 @@ public class AnnotationPersistenceMappingParser return getAttributeOverrideField(attrName, fm, embed); } - private ClassMapping getEmbeddedMapping(ValueMapping val) { + public static ClassMapping getEmbeddedMapping(ValueMapping val) { ClassMapping embed = val.getEmbeddedMapping(); if (embed != null) return embed; @@ -1310,7 +1310,7 @@ public class AnnotationPersistenceMappingParser } - public FieldMapping getAttributeOverrideField(String attrName, FieldMapping fm, ClassMapping embed) { + public static FieldMapping getAttributeOverrideField(String attrName, FieldMapping fm, ClassMapping embed) { FieldMapping efm; int idxOfDot = attrName.indexOf("."); if (idxOfDot == -1) { diff --git a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java index 78f28d934..823059b93 100644 --- a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java +++ b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java @@ -42,6 +42,7 @@ import org.apache.openjpa.jdbc.meta.MappingInfo; import org.apache.openjpa.jdbc.meta.MappingRepository; 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.strats.EnumValueHandler; import org.apache.openjpa.jdbc.meta.strats.FlatClassStrategy; import org.apache.openjpa.jdbc.meta.strats.FullClassStrategy; @@ -661,19 +662,10 @@ public class XMLPersistenceMappingParser /** * Return the proper override. */ - private FieldMapping getAttributeOverride(FieldMapping fm) - throws SAXException { - ClassMapping embed = fm.getEmbeddedMapping(); - if (embed == null) { - embed = fm.getElementMapping().getEmbeddedMapping(); - if (embed == null) - throw getException(_loc.get("not-embedded", fm)); - } - FieldMapping efm = embed.getFieldMapping(_override); - if (efm == null) - throw getException(_loc.get("embed-override-name", - fm, _override)); - return efm; + private FieldMapping getAttributeOverride(FieldMapping fm) + throws SAXException { + return AnnotationPersistenceMappingParser.getEmbeddedFieldMapping(fm, + _override); } /** diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/AddressXml.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/AddressXml.java new file mode 100644 index 000000000..1bb399d54 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/AddressXml.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.embed.attrOverrides; + +public class AddressXml { + protected String street; + protected String city; + protected String state; + protected ZipcodeXml 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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/CustomerXml.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/CustomerXml.java new file mode 100644 index 000000000..ae478d03b --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/CustomerXml.java @@ -0,0 +1,52 @@ +/* + * 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; + +public class CustomerXml { + protected Integer id; + + protected String name; + + protected AddressXml 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 AddressXml getAddress() { + return address; + } + + public void setAddress(AddressXml address) { + this.address = address; + } +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverridesXml.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverridesXml.java new file mode 100644 index 000000000..a35ef5b4f --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverridesXml.java @@ -0,0 +1,98 @@ +/* + * 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.List; + +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; +import javax.persistence.Query; + +import junit.framework.Assert; + +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestAttrOverridesXml extends SingleEMFTestCase { + + public int numPersons = 4; + public List namedQueries = new ArrayList(); + public int eId = 1; + + public void setUp() { + setUp(CLEAR_TABLES); + } + + @Override + protected String getPersistenceUnitName() { + return "embed-pu"; + } + + public void testAttrOverride1() { + createObj1(); + findObj1(); + queryObj1(); + } + + public void createObj1() { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + for (int i = 0; i < numPersons; i++) + createCustomer1(em, eId++); + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public CustomerXml createCustomer1(EntityManager em, int id) { + CustomerXml p = new CustomerXml(); + p.setId(id); + AddressXml addr = new AddressXml(); + 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(); + CustomerXml p = em.find(CustomerXml.class, 1); + Assert.assertEquals(p.getId(), new Integer(1)); + Assert.assertEquals(p.getAddress().getCity(), "city_1"); + Assert.assertEquals(p.getAddress().getStreet(), "street_1"); + Assert.assertEquals(p.getAddress().getState(), "state_1"); + Assert.assertEquals(p.getName(), "name_1"); + } + + public void queryObj1() { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + String jpql = "select p from CustomerXml1 p"; + Query q = em.createQuery(jpql); + List ps = q.getResultList(); + Assert.assertEquals(ps.size(), numPersons); + tran.commit(); + em.close(); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/ZipcodeXml.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/ZipcodeXml.java new file mode 100644 index 000000000..c2ef1a4f4 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/ZipcodeXml.java @@ -0,0 +1,40 @@ +/* + * 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; + +public class ZipcodeXml { + protected String zip; + 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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/embed/embed-orm.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/embed/embed-orm.xml index 5ef817264..8e3ac7465 100644 --- a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/embed/embed-orm.xml +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/embed/embed-orm.xml @@ -149,6 +149,44 @@ version="2.0"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +