OPENJPA-1867: Fix ClassCastException when loading from DataCache.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1030056 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Richard G. Curtis 2010-11-02 14:19:30 +00:00
parent ab326efe07
commit d99b9ba034
2 changed files with 88 additions and 16 deletions

View File

@ -149,7 +149,7 @@ public class DataCacheStoreManager
mods.additions.add(new PCDataHolder(data, sm));
CacheStatistics stats = cache.getStatistics();
if (stats.isEnabled()) {
((CacheStatisticsSPI)stats).newPut(sm.getMetaData().getDescribedType());
((CacheStatisticsSPI)stats).newPut(data.getType());
}
}
}
@ -188,7 +188,7 @@ public class DataCacheStoreManager
}
CacheStatistics stats = cache.getStatistics();
if (stats.isEnabled()) {
((CacheStatisticsSPI)stats).newPut(sm.getMetaData().getDescribedType());
((CacheStatisticsSPI)stats).newPut(data.getType());
}
}
}
@ -339,7 +339,7 @@ public class DataCacheStoreManager
// if we have a cached version update from there
if (version != null) {
if(stats.isEnabled()){
((CacheStatisticsSPI)stats).newGet(sm.getMetaData().getDescribedType(), true);
((CacheStatisticsSPI)stats).newGet(data.getType(), true);
}
if (!version.equals(sm.getVersion())) {
sm.setVersion(version);
@ -349,7 +349,8 @@ public class DataCacheStoreManager
}
if(stats.isEnabled()){
((CacheStatisticsSPI)stats).newGet(sm.getMetaData().getDescribedType(), false);
Class<?> cls = (data == null) ? sm.getMetaData().getDescribedType() : data.getType();
((CacheStatisticsSPI) stats).newGet(cls, false);
}
// use data store version
return super.syncVersion(sm, edata);
@ -360,7 +361,7 @@ public class DataCacheStoreManager
if (cache == null) {
return super.initialize(sm, state, fetch, edata);
}
Class<?> cls = sm.getMetaData().getDescribedType();
DataCachePCData data = cache.get(sm.getObjectId());
CacheStatistics stats = cache.getStatistics();
boolean fromDatabase = false;
@ -373,14 +374,15 @@ public class DataCacheStoreManager
} else {
if (alreadyCached && !isLocking(fetch)) {
if (stats.isEnabled()) {
((CacheStatisticsSPI)stats).newGet(cls, true);
((CacheStatisticsSPI)stats).newGet(data.getType(), true);
}
sm.initialize(cls, state);
sm.initialize(data.getType(), state);
data.load(sm, fetch, edata);
} else {
if (!alreadyCached) {
if (stats.isEnabled()) {
((CacheStatisticsSPI)stats).newGet(cls, false);
// Get the classname from MetaData... but this won't be right in every case.
((CacheStatisticsSPI)stats).newGet(sm.getMetaData().getDescribedType(), false);
}
}
fromDatabase = super.initialize(sm, state, fetch, edata);
@ -391,10 +393,10 @@ public class DataCacheStoreManager
&& ((fetch.getCacheStoreMode() == DataCacheStoreMode.USE && !alreadyCached)
|| (fetch.getCacheStoreMode() == DataCacheStoreMode.REFRESH));
if (updateCache) {
if (stats.isEnabled()) {
((CacheStatisticsSPI)stats).newPut(cls);
}
cacheStateManager(cache, sm, data);
if (stats.isEnabled()) {
((CacheStatisticsSPI) stats).newPut(sm.getMetaData().getDescribedType());
}
}
return fromDatabase || alreadyCached;
}
@ -435,12 +437,12 @@ public class DataCacheStoreManager
return super.load(sm, fields, fetch, lockLevel, edata);
CacheStatistics stats = cache.getStatistics();
Class<?> cls = sm.getMetaData().getDescribedType();
DataCachePCData data = cache.get(sm.getObjectId());
if (lockLevel == LockLevels.LOCK_NONE && !isLocking(fetch) && data != null)
data.load(sm, fields, fetch, edata);
if (fields.length() == 0){
if (stats.isEnabled()) {
Class<?> cls = (data == null) ? sm.getMetaData().getDescribedType() : data.getType();
((CacheStatisticsSPI)stats).newGet(cls, true);
}
return true;
@ -509,7 +511,7 @@ public class DataCacheStoreManager
//### the 'data.type' access here probably needs
//### to be addressed for bug 511
if (stats.isEnabled()) {
((CacheStatisticsSPI)stats).newGet(sm.getMetaData().getDescribedType(), true);
((CacheStatisticsSPI) stats).newGet(data.getType(), true);
}
sm.initialize(data.getType(), state);
data.load(sm, fetch, edata);
@ -529,11 +531,11 @@ public class DataCacheStoreManager
if (fields.length() > 0){
unloaded = addUnloaded(sm, fields, unloaded);
if (stats.isEnabled()) {
((CacheStatisticsSPI)stats).newGet(sm.getMetaData().getDescribedType(), false);
((CacheStatisticsSPI)stats).newGet(data.getType(), false);
}
}else{
if (stats.isEnabled()) {
((CacheStatisticsSPI)stats).newGet(sm.getMetaData().getDescribedType(), true);
((CacheStatisticsSPI)stats).newGet(data.getType(), true);
}
}
} else{
@ -588,7 +590,7 @@ public class DataCacheStoreManager
cache.update(data);
CacheStatistics stats = cache.getStatistics();
if (stats.isEnabled()) {
((CacheStatisticsSPI)stats).newPut(sm.getMetaData().getDescribedType());
((CacheStatisticsSPI)stats).newPut(data.getType());
}
} finally {
cache.writeUnlock();

View File

@ -0,0 +1,70 @@
/*
* 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.inheritance.datacache;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.inheritance.entity.Department;
import org.apache.openjpa.persistence.inheritance.entity.Employee;
import org.apache.openjpa.persistence.inheritance.entity.FTEmployee;
import org.apache.openjpa.persistence.inheritance.entity.Manager;
import org.apache.openjpa.persistence.inheritance.entity.PTEmployee;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestInheritanceWithDataCache extends SingleEMFTestCase {
Object[] props =
new Object[] { FTEmployee.class, Employee.class, Manager.class, PTEmployee.class, Department.class,
"openjpa.DataCache", "true", CLEAR_TABLES };
@Override
public void setUp() throws Exception {
super.setUp(props);
}
public void test() throws Exception {
EntityManager em = emf.createEntityManager();
try {
// Create a manager and a department
em.getTransaction().begin();
Manager m = new Manager();
m.setId(1);
em.persist(m);
Department dept = new Department();
dept.setId(1);
dept.setDepartmentManager(m);
em.persist(dept);
m.setDepartment(dept);
em.getTransaction().commit();
em.clear();
emf.getCache().evictAll();
Employee e = em.find(Employee.class, 1);
assertNotNull(e);
assertTrue(e instanceof Manager);
em.clear();
e = em.find(Employee.class, 1);
assertNotNull(e);
assertTrue(e instanceof Manager);
} finally {
em.close();
}
}
}