OPENJPA-1558:

Check whether the result type is in the same entity hierarchy before using discriminator value when loading a relationship.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@921620 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2010-03-10 23:19:25 +00:00
parent 6234dbddfc
commit dd210a2831
5 changed files with 257 additions and 5 deletions

View File

@ -111,15 +111,26 @@ public abstract class InValueDiscriminatorStrategy
public Class getClass(JDBCStore store, ClassMapping base, Result res)
throws SQLException, ClassNotFoundException {
if (isFinal || !res.contains(disc.getColumns()[0])
|| (base.getPCSuperclass() == null
&& base.getJoinablePCSubclassMappings().length == 0))
if (isFinal
|| !useDiscrimColumn(base, res)
|| (base.getPCSuperclass() == null && base.getJoinablePCSubclassMappings().length == 0)) {
return base.getDescribedType();
}
Object cls =
res.getObject(disc.getColumns()[0], disc.getJavaType(), null);
Object cls = res.getObject(disc.getColumns()[0], disc.getJavaType(), null);
return getClass(cls, store);
}
private final boolean useDiscrimColumn(ClassMapping base, Result res) throws SQLException {
if (res.getBaseMapping() != null && base != null) {
// check whether the result type is assignable to the base mapping.
// if not assignable the discriminator value will not be correct.
if (!base.getDescribedType().isAssignableFrom(res.getBaseMapping().getDescribedType())) {
return false;
}
}
return res.contains(disc.getColumns()[0]);
}
public boolean hasClassConditions(ClassMapping base, boolean subclasses) {
// if selecting the first mapped class and all subclasses, no need
@ -165,3 +176,4 @@ public abstract class InValueDiscriminatorStrategy
return sql;
}
}

View File

@ -0,0 +1,42 @@
/*
* 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.discriminator.fetch;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity(name="D_F_Employee")
@Table(name="D_F_EMPLOYEE")
@DiscriminatorValue(value="E")
public class Employee extends Person {
@ManyToOne(fetch=FetchType.LAZY)
private Manager manager;
public Manager getManager() {
return manager;
}
public void setManager(Manager manager) {
this.manager = manager;
}
}

View File

@ -0,0 +1,43 @@
/*
* 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.discriminator.fetch;
import java.util.Collection;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity(name="D_F_Manager")
@Table(name="D_F_MANAGER")
@DiscriminatorValue(value="M")
public class Manager extends Person {
@OneToMany(fetch=FetchType.LAZY,mappedBy="manager")
Collection<Employee> employees;
public Collection<Employee> getEmployees() {
return employees;
}
public void setEmployees(Collection<Employee> employees) {
this.employees = employees;
}
}

View File

@ -0,0 +1,43 @@
/*
* 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.discriminator.fetch;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity(name="D_F_Person")
@Table(name="D_F_PERSON")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorValue(value="P")
public abstract class Person {
@Id
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}

View File

@ -0,0 +1,112 @@
/*
* 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.discriminator.fetch;
import java.util.HashSet;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import org.apache.openjpa.persistence.test.PersistenceTestCase;
public class TestLazyFetch extends PersistenceTestCase {
private static int N_EMPS = 3;
public EntityManagerFactory newEmf() {
EntityManagerFactory emf = createEMF(Person.class, Employee.class, Manager.class);
assertNotNull("Unable to create EntityManagerFactory", emf);
return emf;
}
public void setUp() {
EntityManagerFactory emf = newEmf();
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
// cleanup from previous execution
tran.begin();
em.createQuery("Delete from D_F_Manager").executeUpdate();
em.createQuery("Delete from D_F_Employee").executeUpdate();
tran.commit();
// populate a small graph.
tran.begin();
Manager m = new Manager();
m.setId(10);
m.setEmployees(new HashSet<Employee>());
em.persist(m);
Employee e;
for (int i = 0; i < N_EMPS; i++) {
e = new Employee();
e.setId(i + 1);
e.setManager(m);
m.getEmployees().add(e);
em.persist(e);
}
tran.commit();
em.close();
emf.close();
}
@SuppressWarnings("unchecked")
public void testFetchOneSideFirst() {
EntityManagerFactory emf = newEmf();
EntityManager em = emf.createEntityManager();
List<Manager> managers = em.createQuery("Select m from D_F_Manager m").getResultList();
assertEquals(1, managers.size());
Manager m = managers.get(0);
List<Employee> emps = em.createQuery("Select e from D_F_Employee e").getResultList();
assertEquals(N_EMPS, emps.size());
for(Employee e : emps) {
assertNotNull(e.getManager());
assertTrue(m.getEmployees().contains(e));
assertEquals(m, e.getManager());
}
em.close();
emf.close();
}
@SuppressWarnings("unchecked")
public void testFetchManySideFirst() {
EntityManagerFactory emf = newEmf();
EntityManager em = emf.createEntityManager();
List<Employee> emps = em.createQuery("Select e from D_F_Employee e").getResultList();
assertEquals(N_EMPS, emps.size());
List<Manager> managers = em.createQuery("Select m from D_F_Manager m").getResultList();
assertEquals(1, managers.size());
Manager m = managers.get(0);
for(Employee e : emps) {
assertNotNull(e.getManager());
assertTrue(m.getEmployees().contains(e));
assertEquals(m, e.getManager());
}
em.close();
emf.close();
}
}