OPENJPA-1556: fix @Strategy inside Embeddable

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@921436 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2010-03-10 16:29:02 +00:00
parent 137ad1bacc
commit 2798b0dd73
4 changed files with 198 additions and 3 deletions

View File

@ -22,6 +22,7 @@ import java.lang.reflect.Modifier;
import org.apache.openjpa.jdbc.meta.strats.NoneFieldStrategy;
import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.meta.ValueMetaData;
import org.apache.openjpa.util.MetaDataException;
/**
@ -56,7 +57,10 @@ public class RuntimeStrategyInstaller
}
public void installStrategy(FieldMapping field) {
FieldStrategy strategy = repos.namedStrategy(field, true);
FieldStrategy strategy = null;
ClassMapping owner = getOutermostDefiningMapping(field);
if (owner != null && !owner.isEmbeddable())
strategy = repos.namedStrategy(field, true);
if (strategy == null) {
try {
strategy = repos.defaultStrategy(field, true, false);
@ -77,6 +81,20 @@ public class RuntimeStrategyInstaller
}
field.setStrategy(strategy, Boolean.FALSE);
}
private ClassMapping getOutermostDefiningMapping(ValueMetaData vm) {
if (vm instanceof FieldMapping) {
ClassMapping owner = ((FieldMapping)vm).getDefiningMapping();
ValueMetaData val = owner.getEmbeddingMetaData();
if (val == null)
return owner;
return getOutermostDefiningMapping(val);
} else if (vm instanceof ValueMappingImpl) {
FieldMapping owner = ((ValueMappingImpl)vm).getFieldMapping();
return getOutermostDefiningMapping(owner);
}
return null;
}
public void installStrategy(Version version) {
VersionStrategy strat = repos.namedStrategy(version);

View File

@ -0,0 +1,75 @@
/*
* 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.jdbc.annotations;
import java.awt.Point;
import javax.persistence.*;
import org.apache.openjpa.persistence.Persistent;
import org.apache.openjpa.persistence.jdbc.Strategy;
/**
* Same as EmbedValue but no blobs for suitable use as key/value/element
* in oracle.
*/
@Embeddable
public class EmbedValue3 {
@Basic
@Column(name = "EMB_BASIC")
protected String basic;
@Basic
@Column(name = "EMB_INTBASIC")
protected int intBasic;
@Persistent
@Strategy("PointHandler")
@Column(name="my_point")
private Point point;
public void setBasic(String basic) {
this.basic = basic;
}
public String getBasic() {
return basic;
}
public void setIntBasic(int intBasic) {
this.intBasic = intBasic;
}
public int getIntBasic() {
return intBasic;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
}

View File

@ -0,0 +1,72 @@
/*
* 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.jdbc.annotations;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "NONSTD_ENTITY3")
public class NonstandardMappingEntity3 {
@Id
private long id;
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name="EmbedVal3s")
private List<EmbedValue3> embedVal3s = new ArrayList<EmbedValue3>();
@Embedded
private EmbedValue3 embedVal3;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public List<EmbedValue3> getEmbedVal3s() {
return embedVal3s;
}
public void setEmbedVal3s(List<EmbedValue3> embedVal3s) {
this.embedVal3s = embedVal3s;
}
public EmbedValue3 getEmbedVal3() {
return embedVal3;
}
public void setEmbedVal3(EmbedValue3 embedVal3) {
this.embedVal3 = embedVal3;
}
}

View File

@ -45,8 +45,8 @@ public class TestNonstandardMappingAnnotations
private DBDictionary _dict;
public void setUp() {
setUp(NonstandardMappingEntity.class, ExtensionsEntity.class,
NonstandardMappingMappedSuper.class, EmbedValue2.class,
setUp(NonstandardMappingEntity.class, NonstandardMappingEntity3.class, ExtensionsEntity.class,
NonstandardMappingMappedSuper.class, EmbedValue2.class, EmbedValue3.class,
EmbedValue.class,
CLEAR_TABLES, RETAIN_DATA);
@ -277,4 +277,34 @@ public class TestNonstandardMappingAnnotations
assertEquals("basic", pc.getEmbedCollection().get(0).getBasic());
em.close();
}
public void testInsertAndRetrieveEmbeddedObjectWithStrategy() {
NonstandardMappingEntity3 pc = new NonstandardMappingEntity3();
EmbedValue3 embed3 = new EmbedValue3();
embed3.setBasic("basic");
Point point = new Point();
point.setLocation(1, 2);
embed3.setPoint(point);
pc.getEmbedVal3s().add(embed3);
pc.setEmbedVal3(embed3);
pc.setId(1);
OpenJPAEntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(pc);
em.getTransaction().commit();
Object pcId = em.getObjectId(pc);
em.close();
em = emf.createEntityManager();
pc = em.find(NonstandardMappingEntity3.class, pcId);
assertEquals(1, pc.getEmbedVal3s().size());
assertEquals("basic", pc.getEmbedVal3s().get(0).getBasic());
assertEquals(1.0, pc.getEmbedVal3s().get(0).getPoint().getX());
assertEquals(2.0, pc.getEmbedVal3s().get(0).getPoint().getY());
assertEquals(1.0, pc.getEmbedVal3().getPoint().getX());
assertEquals(2.0, pc.getEmbedVal3().getPoint().getY());
em.close();
}
}