OPENJPA-2586: Fix to return proper relationship data when QueryCache and FetchPlans are used.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.1.x@1702142 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Heath Thomann 2015-09-10 01:36:41 +00:00
parent 8de9eed223
commit 1eba6fd556
8 changed files with 547 additions and 25 deletions

View File

@ -112,7 +112,7 @@ public class QueryCacheStoreQuery
* READ_SERIALIZABLE -- to do so, we'd just return false when in
* a transaction.
*/
private List<Object> checkCache(QueryKey qk) {
private List<Object> checkCache(QueryKey qk, FetchConfiguration loadFc) {
if (qk == null)
return null;
FetchConfiguration fetch = getContext().getFetchConfiguration();
@ -122,9 +122,7 @@ public class QueryCacheStoreQuery
return null;
// get the cached data
QueryResult res = _cache.get(qk);
QueryResult res = _cache.get(qk);
if (res == null) {
return null;
}
@ -165,7 +163,7 @@ public class QueryCacheStoreQuery
return null;
}
}
return new CachedList(res, projs != 0, _sctx);
return new CachedList(res, projs != 0, _sctx, loadFc);
}
/**
@ -183,7 +181,7 @@ public class QueryCacheStoreQuery
/**
* Copy a projection element for caching / returning.
*/
private static Object copyProjection(Object obj, StoreContext ctx) {
private static Object copyProjection(Object obj, StoreContext ctx, FetchConfiguration fc) {
if (obj == null)
return null;
switch (JavaTypes.getTypeCode(obj.getClass())) {
@ -206,7 +204,7 @@ public class QueryCacheStoreQuery
return ((Locale) obj).clone();
default:
if (obj instanceof CachedObjectId)
return fromObjectId(((CachedObjectId) obj).oid, ctx);
return fromObjectId(((CachedObjectId) obj).oid, ctx, fc);
Object oid = ctx.getObjectId(obj);
if (oid != null)
return new CachedObjectId(oid);
@ -217,11 +215,11 @@ public class QueryCacheStoreQuery
/**
* Return the result object based on its cached oid.
*/
private static Object fromObjectId(Object oid, StoreContext sctx) {
private static Object fromObjectId(Object oid, StoreContext sctx, FetchConfiguration fc) {
if (oid == null)
return null;
Object obj = sctx.find(oid, null, null, null, 0);
Object obj = sctx.find(oid, fc, null, null, 0);
if (obj == null)
throw new ObjectNotFoundException(oid);
return obj;
@ -332,19 +330,41 @@ public class QueryCacheStoreQuery
_fc = fc;
}
public ResultObjectProvider executeQuery(StoreQuery q, Object[] params,
Range range) {
public ResultObjectProvider executeQuery(StoreQuery q, Object[] params, Range range) {
QueryCacheStoreQuery cq = (QueryCacheStoreQuery) q;
Object parsed = cq.getDelegate().getCompilation();
QueryKey key = QueryKey.newInstance(cq.getContext(),
_ex.isPacking(q), params, _candidate, _subs, range.start,
range.end, parsed);
List<Object> cached = cq.checkCache(key);
if (cached != null)
return new ListResultObjectProvider(cached);
QueryKey key =
QueryKey.newInstance(cq.getContext(), _ex.isPacking(q), params, _candidate, _subs, range.start,
range.end, parsed);
ResultObjectProvider rop = _ex.executeQuery(cq.getDelegate(),
params, range);
// Create a new FetchConfiguration that will be used to ensure that any JOIN FETCHed fields are loaded
StoreContext store = q.getContext().getStoreContext();
FetchConfiguration cacheFc = store.pushFetchConfiguration();
// OPENJPA-2586: If the FetchConfig for this executor contains fields,
// then add them to the new FetchConfig.
if (!_fc.getFields().isEmpty()) {
cacheFc.addFields(_fc.getFields());
}
for (QueryExpressions qe : _ex.getQueryExpressions()) {
for (String fetchFields : qe.fetchPaths) {
cacheFc.addField(fetchFields);
}
for (String fetchFields : qe.fetchInnerPaths) {
cacheFc.addField(fetchFields);
}
}
try {
List<Object> cached = cq.checkCache(key, cacheFc);
if (cached != null) {
return new ListResultObjectProvider(cached);
}
} finally {
store.popFetchConfiguration();
}
ResultObjectProvider rop = _ex.executeQuery(cq.getDelegate(), params, range);
if (_fc.getQueryCacheEnabled())
return cq.wrapResult(rop, key);
else
@ -491,7 +511,7 @@ public class QueryCacheStoreQuery
}
/**
* Result list implementation for a cached query result. Package-protected
* Result list implementation for a cached query result. Public
* for testing.
*/
public static class CachedList extends AbstractList<Object>
@ -500,23 +520,25 @@ public class QueryCacheStoreQuery
private final QueryResult _res;
private final boolean _proj;
private final StoreContext _sctx;
public CachedList(QueryResult res, boolean proj, StoreContext ctx) {
private final FetchConfiguration _fc;
public CachedList(QueryResult res, boolean proj, StoreContext ctx, FetchConfiguration fc) {
_res = res;
_proj = proj;
_sctx = ctx;
_fc = fc;
}
public Object get(int idx) {
if (!_proj)
return fromObjectId(_res.get(idx), _sctx);
return fromObjectId(_res.get(idx), _sctx, _fc);
Object[] cached = (Object[]) _res.get(idx);
if (cached == null)
return null;
Object[] uncached = new Object[cached.length];
for (int i = 0; i < cached.length; i++)
uncached[i] = copyProjection(cached[i], _sctx);
uncached[i] = copyProjection(cached[i], _sctx, _fc);
return uncached;
}
@ -601,7 +623,7 @@ public class QueryCacheStoreQuery
Object[] arr = (Object[]) obj;
Object[] cp = new Object[arr.length];
for (int i = 0; i < arr.length; i++)
cp[i] = copyProjection(arr[i], _sctx);
cp[i] = copyProjection(arr[i], _sctx, null);
cached = cp;
}
if (cached != null)

View File

@ -0,0 +1,105 @@
/*
* 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.jpql.joins;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;
/**
* Entity implementation class for Entity: Parent
*
*/
@Entity
@Table(name="FETCHDEPT")
public class Department implements Serializable {
private static final long serialVersionUID = -5537435298484817651L;
@Id
private int deptno;
@Version
private int version;
private String name;
@OneToMany(mappedBy="dept", cascade=CascadeType.ALL)
private List<Employee> employees;
@OneToMany(mappedBy="dept", cascade=CascadeType.ALL)
private List<Employee> employee2s;
public Department() {
super();
}
public Department(int deptno, String name) {
super();
this.deptno = deptno;
this.name = name;
}
public int getDeptno() {
return this.deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public int getVersion() {
return this.version;
}
public void setVersion(int version) {
this.version = version;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmployees() {
return this.employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
public List<Employee> getEmployee2s() {
return this.employee2s;
}
public void setEmployee2s(List<Employee> employees) {
this.employee2s = employees;
}
public String toString() {
return "[Department:depno=" + deptno + ", version=" + version + ", name=" + name +
", employees=" + employees + ", employee2s=" + employee2s+ ']';
}
}

View File

@ -0,0 +1,36 @@
/*
* 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.jpql.joins;
import java.lang.Integer;
import java.lang.String;
import javax.persistence.metamodel.ListAttribute;
import javax.persistence.metamodel.SingularAttribute;
@javax.persistence.metamodel.StaticMetamodel
(value=org.apache.openjpa.persistence.jpql.joins.Department.class)
@javax.annotation.Generated
(value="org.apache.openjpa.persistence.meta.AnnotationProcessor6",date="Tue Jun 03 09:13:08 CDT 2014")
public class Department_ {
public static volatile SingularAttribute<Department,Integer> deptno;
public static volatile ListAttribute<Department,Employee> employee2s;
public static volatile ListAttribute<Department,Employee> employees;
public static volatile SingularAttribute<Department,String> name;
public static volatile SingularAttribute<Department,Integer> version;
}

View File

@ -0,0 +1,91 @@
/*
* 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.jpql.joins;
import java.io.Serializable;
import java.lang.String;
import javax.persistence.*;
/**
* Entity implementation class for Entity: Child
*
*/
@Entity
@Table(name = "FETCHEMPL")
public class Employee implements Serializable {
private static final long serialVersionUID = -5155314943010802723L;
@Id
private int empno;
private String name;
@Version
private int version;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
private Department dept;
public Employee() {
super();
}
public Employee(int empno, String name, Department dept) {
super();
this.empno = empno;
this.name = name;
this.dept = dept;
}
public int getEmpno() {
return this.empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public int getVersion() {
return this.version;
}
public void setVersion(int version) {
this.version = version;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
public String toString() {
return "[Employee:id=" + empno + ", version=" + version + ", name="
+ name + ']';
}
}

View File

@ -0,0 +1,34 @@
/*
* 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.jpql.joins;
import java.lang.Integer;
import java.lang.String;
import javax.persistence.metamodel.SingularAttribute;
@javax.persistence.metamodel.StaticMetamodel
(value=org.apache.openjpa.persistence.jpql.joins.Employee.class)
@javax.annotation.Generated
(value="org.apache.openjpa.persistence.meta.AnnotationProcessor6",date="Tue Jun 03 09:14:37 CDT 2014")
public class Employee_ {
public static volatile SingularAttribute<Employee,Department> dept;
public static volatile SingularAttribute<Employee,Integer> empno;
public static volatile SingularAttribute<Employee,String> name;
public static volatile SingularAttribute<Employee,Integer> version;
}

View File

@ -0,0 +1,89 @@
/*
* 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.jpql.joins;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import junit.framework.Assert;
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
public class TestJoinFetchWithQueryDataCache extends SQLListenerTestCase {
EntityManager em;
public void setUp() {
super.setUp(DROP_TABLES, Employee.class, Department.class, "openjpa.QueryCompilationCache", "all",
"openjpa.DataCache", "true", "openjpa.RemoteCommitProvider", "sjvm", "openjpa.QueryCache", "true"
);
em = emf.createEntityManager();
em.getTransaction().begin();
Department dept;
dept = new Department(10, "department 10");
dept.setEmployees(new ArrayList<Employee>());
dept.getEmployees().add(new Employee(11, "Emp11", dept));
dept.getEmployees().add(new Employee(12, "Emp12", dept));
dept.setEmployee2s(new ArrayList<Employee>());
dept.getEmployee2s().add(new Employee(211, "Emp211", dept));
dept.getEmployee2s().add(new Employee(212, "Emp212", dept));
em.persist(dept);
dept = new Department(20, "department 20");
dept.setEmployees(new ArrayList<Employee>());
dept.getEmployees().add(new Employee(21, "Emp21", dept));
dept.getEmployees().add(new Employee(22, "Emp22", dept));
dept.setEmployee2s(new ArrayList<Employee>());
dept.getEmployee2s().add(new Employee(221, "Emp221", dept));
dept.getEmployee2s().add(new Employee(222, "Emp222", dept));
em.persist(dept);
em.getTransaction().commit();
em.close();
}
public void testConsecutiveJPQLJoinFetchCall() {
doQuery(emf, false);
doQuery(emf, true);
}
private void doQuery(EntityManagerFactory emf, boolean cached) {
String query = "select o from Employee o " + "left join fetch o.dept " + "where o.dept.deptno = 10";
EntityManager em = emf.createEntityManager();
sql.clear();
List<Employee> emps = em.createQuery(query, Employee.class).getResultList();
Assert.assertEquals(4, emps.size());
for (Employee emp : emps) {
em.detach(emp);
Assert.assertNotNull(emp.getDept());
Assert.assertEquals(2, emp.getDept().getEmployees().size());
}
em.close();
if (cached) {
assertTrue(sql.size() == 0);
}
}
}

View File

@ -0,0 +1,57 @@
/*
* 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.querycache;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@Entity
public class QCEntityM2O {
@Id
@Column(name = "PK")
private String pk;
@ManyToOne(fetch = FetchType.LAZY)
private QCEntity qc;
public QCEntityM2O(String pk) {
this.pk = pk;
}
public String getPk() {
return pk;
}
public void setPk(String pk) {
this.pk = pk;
}
public void setQc(QCEntity qc) {
this.qc = qc;
}
public QCEntity getQc() {
return qc;
}
}

View File

@ -0,0 +1,88 @@
/*
* 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.querycache;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.apache.openjpa.persistence.FetchPlan;
import org.apache.openjpa.persistence.OpenJPAQuery;
import org.apache.openjpa.persistence.querycache.QCEntityM2O;
import org.apache.openjpa.persistence.querycache.QCEntity;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestQueryCacheWithDataCache extends SingleEMFTestCase {
public void setUp() {
super.setUp(DROP_TABLES, QCEntityM2O.class, QCEntity.class, "openjpa.DataCache", "true",
"openjpa.RemoteCommitProvider", "sjvm", "openjpa.QueryCache", "true");
}
/*
* Test for OPENJPA-2586
*/
public void testWithFetchPlan() {
populate();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
doQueryWithFetchPlan(em);
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
doQueryWithFetchPlan(em);
em.getTransaction().commit();
em.close();
}
public void doQueryWithFetchPlan(EntityManager em) {
String jpql = "Select e1 from QCEntityM2O e1";
Query q = em.createQuery(jpql);
FetchPlan fetchPlan = q.unwrap(OpenJPAQuery.class).getFetchPlan();
fetchPlan.addField(QCEntityM2O.class, "qc");
List<QCEntityM2O> results = (List<QCEntityM2O>) q.getResultList();
em.clear();
assertTrue("No results returned!", !results.isEmpty());
for (QCEntityM2O e1 : results) {
assertNotNull("A 'QCEntity' should have been returned!", e1.getQc());
}
}
public void populate() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
QCEntityM2O e1 = new QCEntityM2O("aQCEntityM2O");
QCEntity e2 = new QCEntity("aQCEntityM2O", "test", 2L);
e1.setQc(e2);
em.persist(e1);
em.persist(e2);
em.getTransaction().commit();
em.close();
}
}