OPENJPA-930: orm xml support for AttributeOverride

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@747239 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-02-24 00:39:48 +00:00
parent 9d7360cb35
commit 62c76e42ea
7 changed files with 286 additions and 16 deletions

View File

@ -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) {

View File

@ -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);
}
/**

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<String> namedQueries = new ArrayList<String>();
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<CustomerXml> ps = q.getResultList();
Assert.assertEquals(ps.size(), numPersons);
tran.commit();
em.close();
}
}

View File

@ -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;
}
}

View File

@ -149,6 +149,44 @@ version="2.0">
</attributes>
</entity>
<entity name="CustomerXml1"
class="org.apache.openjpa.persistence.embed.attrOverrides.CustomerXml"
access="FIELD">
<attributes>
<id name="id"><column length="12"/></id>
<basic name="name"><column length="12"/></basic>
<embedded name="address">
<attribute-override name="state">
<column name="ADDR_STATE" />
</attribute-override>
<attribute-override name="zipcode.zip">
<column name="ADDR_ZIP" />
</attribute-override>
</embedded>
</attributes>
</entity>
<embeddable
class="org.apache.openjpa.persistence.embed.attrOverrides.AddressXml"
access="FIELD">
<attributes>
<basic name="street"><column length="12"/>
</basic>
<basic name="city"><column length="12"/></basic>
<basic name="state"><column length="12"/></basic>
<embedded name="zipcode"/>
</attributes>
</embeddable>
<embeddable
class="org.apache.openjpa.persistence.embed.attrOverrides.ZipcodeXml"
access="FIELD">
<attributes>
<basic name="zip"><column length="12"/></basic>
<basic name="plusFour"><column length="12"/></basic>
</attributes>
</embeddable>
<embeddable
class="org.apache.openjpa.persistence.embed.Embed_EmbedXml"
access="FIELD">