OPENJPA-1028: fixed setMappedByValue when embeddable is involved.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@763427 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-04-08 22:06:56 +00:00
parent e4ec2cf90d
commit 2901ea6e52
5 changed files with 267 additions and 4 deletions

View File

@ -54,7 +54,6 @@ import org.apache.openjpa.jdbc.sql.Select;
import org.apache.openjpa.jdbc.sql.SelectExecutor;
import org.apache.openjpa.jdbc.sql.Union;
import org.apache.openjpa.kernel.OpenJPAStateManager;
import org.apache.openjpa.kernel.StateManagerImpl;
import org.apache.openjpa.lib.log.Log;
import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.meta.ClassMetaData;
@ -660,7 +659,7 @@ public class RelationFieldStrategy
// By saving the mapped-by info in 'res' is to
// avoid unneeded SQL pushdown that would otherwise gets
// generated.
if (decMeta != null) {
if (decMeta != null && !sm.isEmbedded()) {
mappedByValue = sm.getPersistenceCapable();
res.setMappedByFieldMapping(mappedByFieldMapping);
res.setMappedByValue(mappedByValue);

View File

@ -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;
import javax.persistence.Embeddable;
import javax.persistence.OneToOne;
@Embeddable
public class Embed_MappedToOne {
protected String name1;
protected String name2;
protected String name3;
@OneToOne(mappedBy="entityA")
protected EntityB1 bm;
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
public String getName2() {
return name2;
}
public void setName2(String name2) {
this.name2 = name2;
}
public String getName3() {
return name3;
}
public void setName3(String name3) {
this.name3 = name3;
}
public void setMappedEntityB(EntityB1 bm) {
this.bm = bm;
}
public EntityB1 getMappedEntityB() {
return bm;
}
}

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.embed;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
@Entity
public class EntityA_Embed_MappedToOne implements Serializable {
@Id
Integer id;
@Column(length=30)
String name;
@Basic(fetch=FetchType.LAZY)
int age;
@Embedded
protected Embed_MappedToOne embed;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
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 Embed_MappedToOne getEmbed() {
return embed;
}
public void setEmbed(Embed_MappedToOne embed) {
this.embed = embed;
}
}

View File

@ -22,6 +22,7 @@ import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class EntityB1 implements Serializable {
@ -31,6 +32,9 @@ public class EntityB1 implements Serializable {
@Column(length=30)
String name;
@OneToOne
EntityA_Embed_MappedToOne entityA;
public int getId() {
return id;
@ -47,5 +51,13 @@ public class EntityB1 implements Serializable {
public void setName(String name) {
this.name = name;
}
public EntityA_Embed_MappedToOne getEntityA() {
return entityA;
}
public void setEntityA(EntityA_Embed_MappedToOne entityA) {
this.entityA = entityA;
}
}

View File

@ -78,8 +78,8 @@ public class TestEmbeddable extends SingleEMFTestCase {
Employee2.class, EmployeePK2.class, Department3.class,
Employee3.class, EmployeeName3.class, Item1.class, Item2.class,
Item3.class, Company1.class, Company2.class, Division.class,
VicePresident.class,
DROP_TABLES);
VicePresident.class, EntityA_Embed_MappedToOne.class,
Embed_MappedToOne.class, DROP_TABLES);
}
public void testEntityA_Coll_String() {
@ -94,6 +94,12 @@ public class TestEmbeddable extends SingleEMFTestCase {
findEntityA_Embed_ToOne();
}
public void testEntityA_Embed_MappedToOne() {
createEntityA_Embed_MappedToOne();
queryEntityA_Embed_MappedToOne();
findEntityA_Embed_MappedToOne();
}
public void testEntityA_Coll_Embed_ToOne() {
createEntityA_Coll_Embed_ToOne();
queryEntityA_Coll_Embed_ToOne();
@ -213,6 +219,44 @@ public class TestEmbeddable extends SingleEMFTestCase {
return embed;
}
/*
* Create EntityA_Embed_MappedToOne
*/
public void createEntityA_Embed_MappedToOne() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
createEntityA_Embed_MappedToOne(em, ID);
tran.begin();
em.flush();
tran.commit();
em.close();
}
public void createEntityA_Embed_MappedToOne(EntityManager em, int id) {
EntityA_Embed_MappedToOne a = new EntityA_Embed_MappedToOne();
a.setId(id);
a.setName("a" + id);
a.setAge(id);
Embed_MappedToOne embed = createEmbed_MappedToOne(em, id, a);
a.setEmbed(embed);
em.persist(a);
}
public Embed_MappedToOne createEmbed_MappedToOne(EntityManager em, int id,
EntityA_Embed_MappedToOne a) {
Embed_MappedToOne embed = new Embed_MappedToOne();
embed.setName1("name1");
embed.setName2("name2");
embed.setName3("name3");
EntityB1 b = new EntityB1();
b.setId(id);
b.setName("bm" + id);
b.setEntityA(a);
embed.setMappedEntityB(b);
em.persist(b);
return embed;
}
/*
* Create EntityA_Coll_Embed_ToOne
*/
@ -670,6 +714,16 @@ public class TestEmbeddable extends SingleEMFTestCase {
em.close();
}
/*
* Find EntityA_Embed_MappedToOne
*/
public void findEntityA_Embed_MappedToOne() {
EntityManager em = emf.createEntityManager();
EntityA_Embed_MappedToOne a = em.find(EntityA_Embed_MappedToOne.class, ID);
checkEntityA_Embed_MappedToOne(a);
em.close();
}
/*
* Find EntityA_Coll_Embed_ToOne
*/
@ -769,6 +823,20 @@ public class TestEmbeddable extends SingleEMFTestCase {
checkEmbed_ToOne(embed);
}
/*
* check EntityA_Embed_MappedToOne
*/
public void checkEntityA_Embed_MappedToOne(EntityA_Embed_MappedToOne a) {
int id = a.getId();
String name = a.getName();
int age = a.getAge();
assertEquals(1, id);
assertEquals("a" + id ,name);
assertEquals(1, age);
Embed_MappedToOne embed = a.getEmbed();
checkEmbed_MappedToOne(embed);
}
/*
* check EntityA_Coll_Embed_ToOne
*/
@ -796,6 +864,18 @@ public class TestEmbeddable extends SingleEMFTestCase {
assertEquals("b" + b.getId(), b.getName());
}
public void checkEmbed_MappedToOne(Embed_MappedToOne embed) {
String name1 = embed.getName1();
String name2 = embed.getName2();
String name3 = embed.getName3();
assertEquals("name1", name1);
assertEquals("name2", name2);
assertEquals("name3", name3);
EntityB1 b = embed.getMappedEntityB();
assertEquals(1, b.getId());
assertEquals("bm" + b.getId(), b.getName());
}
/*
* check EntityA_Embed_ToMany
*/
@ -1012,6 +1092,38 @@ public class TestEmbeddable extends SingleEMFTestCase {
em.close();
}
/*
* Query EntityA_Embed_MappedToOne
*/
public void queryEntityA_Embed_MappedToOne() {
EntityManager em = emf.createEntityManager();
// test select embed object
String[] query = {
"select a.embed from " +
" EntityA_Embed_MappedToOne a ",
"select e from EntityA_Embed_MappedToOne a " +
" join a.embed e join e.bm bm where e.bm.id > 0 order by a.id",
};
for (int i = 0; i < query.length; i++) {
List<Object[]> rs = null;
rs = em.createQuery(query[i]).getResultList();
assertTrue(rs.size() > 0);
Object obj = rs.get(0);
assertTrue(obj instanceof Embed_MappedToOne);
assertTrue(((Embed_MappedToOne) obj).getMappedEntityB() != null);
em.clear();
}
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select a from EntityA_Embed_MappedToOne a");
List<EntityA_Embed_MappedToOne> as = q.getResultList();
for (EntityA_Embed_MappedToOne a : as) {
checkEntityA_Embed_MappedToOne(a);
}
tran.commit();
em.close();
}
/*
* Query EntityA_Coll_Embed_ToOne
*/