mirror of https://github.com/apache/openjpa.git
Fix StoreFacadeTypeRegistry to work when multiple stores are available. Fix
problems with creating a persistence FetchPlan, and with getting max depth. git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@428069 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
20d5990098
commit
b2e1912dc7
|
@ -492,8 +492,8 @@ public class RelationFieldStrategy
|
||||||
long id = res.getLong(cols[0]);
|
long id = res.getLong(cols[0]);
|
||||||
if (!res.wasNull())
|
if (!res.wasNull())
|
||||||
oid = store.newDataStoreId(id, relMapping, true);
|
oid = store.newDataStoreId(id, relMapping, true);
|
||||||
} else // application id
|
} else {
|
||||||
{
|
// application id
|
||||||
if (cols.length == 1) {
|
if (cols.length == 1) {
|
||||||
Object val = res.getObject(cols[0], null, null);
|
Object val = res.getObject(cols[0], null, null);
|
||||||
if (val != null)
|
if (val != null)
|
||||||
|
|
|
@ -13,11 +13,60 @@ public class StoreFacadeTypeRegistry {
|
||||||
|
|
||||||
private Map _impls = new ConcurrentHashMap();
|
private Map _impls = new ConcurrentHashMap();
|
||||||
|
|
||||||
public void registerImplementation(Class facadeType, Class implType) {
|
/**
|
||||||
_impls.put(facadeType, implType);
|
* Register a facade implementation.
|
||||||
|
*
|
||||||
|
* @param facadeType the facade interface
|
||||||
|
* @param storeType the store's
|
||||||
|
* {@link org.apache.openjpa.kernel.StoreManager} type, or null for generic
|
||||||
|
* @param implType the class implementing the facade
|
||||||
|
*/
|
||||||
|
public void registerImplementation(Class facadeType, Class storeType,
|
||||||
|
Class implType) {
|
||||||
|
Object key = (storeType == null) ? (Object)facadeType
|
||||||
|
: new Key(facadeType, storeType);
|
||||||
|
_impls.put(key, implType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Class getImplementation(Class facadeType) {
|
/**
|
||||||
return (Class) _impls.get(facadeType);
|
* Return the implementation for the given facade and store.
|
||||||
|
*
|
||||||
|
* @param facadeType the facade interface
|
||||||
|
* @param storeType the store's
|
||||||
|
* {@link org.apache.openjpa.kernel.StoreManager} type, or null for generic
|
||||||
|
* @param implType the registered implementor
|
||||||
|
*/
|
||||||
|
public Class getImplementation(Class facadeType, Class storeType) {
|
||||||
|
Object key = (storeType == null) ? (Object)facadeType
|
||||||
|
: new Key(facadeType, storeType);
|
||||||
|
Class c = (Class) _impls.get(key);
|
||||||
|
// if no store-specific type, see if there is a generic avaialble
|
||||||
|
if (c == null && storeType != null)
|
||||||
|
c = (Class) _impls.get(facadeType);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lookup key for facade+store hash.
|
||||||
|
*/
|
||||||
|
private static class Key {
|
||||||
|
private final Class _facadeType;
|
||||||
|
private final Class _storeType;
|
||||||
|
|
||||||
|
public Key(Class facadeType, Class storeType) {
|
||||||
|
_facadeType = facadeType;
|
||||||
|
_storeType = storeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int hashCode() {
|
||||||
|
return _facadeType.hashCode() ^ _storeType.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (other == this)
|
||||||
|
return true;
|
||||||
|
Key k = (Key) other;
|
||||||
|
return _facadeType == k._facadeType && _storeType == k._storeType;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ package org.apache.openjpa.persistence.jdbc;
|
||||||
import org.apache.openjpa.conf.OpenJPAConfiguration;
|
import org.apache.openjpa.conf.OpenJPAConfiguration;
|
||||||
import org.apache.openjpa.conf.ProductDerivation;
|
import org.apache.openjpa.conf.ProductDerivation;
|
||||||
import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
|
import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
|
||||||
|
import org.apache.openjpa.jdbc.kernel.JDBCStoreManager;
|
||||||
import org.apache.openjpa.lib.conf.ConfigurationProvider;
|
import org.apache.openjpa.lib.conf.ConfigurationProvider;
|
||||||
import org.apache.openjpa.persistence.FetchPlan;
|
import org.apache.openjpa.persistence.FetchPlan;
|
||||||
import org.apache.openjpa.persistence.PersistenceProductDerivation;
|
import org.apache.openjpa.persistence.PersistenceProductDerivation;
|
||||||
|
@ -39,8 +40,8 @@ public class JDBCPersistenceProductDerivation
|
||||||
}
|
}
|
||||||
|
|
||||||
public void beforeConfigurationLoad(OpenJPAConfiguration c) {
|
public void beforeConfigurationLoad(OpenJPAConfiguration c) {
|
||||||
c.getStoreFacadeTypeRegistry().registerImplementation(
|
c.getStoreFacadeTypeRegistry().registerImplementation(FetchPlan.class,
|
||||||
FetchPlan.class, JDBCFetchPlan.class);
|
JDBCStoreManager.class, JDBCFetchPlan.class);
|
||||||
if (!(c instanceof JDBCConfigurationImpl))
|
if (!(c instanceof JDBCConfigurationImpl))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ import javax.persistence.Persistence;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import junit.textui.TestRunner;
|
import junit.textui.TestRunner;
|
||||||
|
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple test case to get an EntityManager and perform some basic operations.
|
* Simple test case to get an EntityManager and perform some basic operations.
|
||||||
|
@ -69,6 +70,12 @@ public class TestPersistence
|
||||||
t.setRollbackOnly();
|
t.setRollbackOnly();
|
||||||
t.rollback();
|
t.rollback();
|
||||||
|
|
||||||
|
// openjpa-facade test
|
||||||
|
assertTrue(em instanceof OpenJPAEntityManager);
|
||||||
|
OpenJPAEntityManager ojem = (OpenJPAEntityManager) em;
|
||||||
|
ojem.getFetchPlan().setMaxFetchDepth(-1);
|
||||||
|
assertEquals(-1, ojem.getFetchPlan().getMaxFetchDepth());
|
||||||
|
|
||||||
em.close();
|
em.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -259,7 +259,7 @@ public class EntityManagerFactoryImpl
|
||||||
* Create a store-specific facade for the given fetch configuration.
|
* Create a store-specific facade for the given fetch configuration.
|
||||||
* If no facade class exists, we use the default {@link FetchPlan}.
|
* If no facade class exists, we use the default {@link FetchPlan}.
|
||||||
*/
|
*/
|
||||||
FetchPlan toFetchPlan(FetchConfiguration fetch) {
|
FetchPlan toFetchPlan(Broker broker, FetchConfiguration fetch) {
|
||||||
if (fetch == null)
|
if (fetch == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
@ -268,15 +268,16 @@ public class EntityManagerFactoryImpl
|
||||||
inner = ((DelegatingFetchConfiguration) inner).
|
inner = ((DelegatingFetchConfiguration) inner).
|
||||||
getInnermostDelegate();
|
getInnermostDelegate();
|
||||||
|
|
||||||
_factory.lock();
|
|
||||||
try {
|
try {
|
||||||
if (_plan == null) {
|
if (_plan == null) {
|
||||||
Class cls = _factory.getConfiguration()
|
Class storeType = (broker == null) ? null : broker.
|
||||||
.getStoreFacadeTypeRegistry().getImplementation(
|
getStoreManager().getInnermostDelegate().getClass();
|
||||||
FetchPlan.class);
|
Class cls = _factory.getConfiguration().
|
||||||
|
getStoreFacadeTypeRegistry().
|
||||||
|
getImplementation(FetchPlan.class, storeType);
|
||||||
if (cls == null)
|
if (cls == null)
|
||||||
cls = FetchPlan.class;
|
cls = FetchPlan.class;
|
||||||
_plan = cls.getConstructor(FetchPlan.class);
|
_plan = cls.getConstructor(FetchConfiguration.class);
|
||||||
}
|
}
|
||||||
return _plan.newInstance(fetch);
|
return _plan.newInstance(fetch);
|
||||||
} catch (InvocationTargetException ite) {
|
} catch (InvocationTargetException ite) {
|
||||||
|
@ -284,8 +285,6 @@ public class EntityManagerFactoryImpl
|
||||||
(ite.getTargetException());
|
(ite.getTargetException());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw PersistenceExceptions.toPersistenceException(e);
|
throw PersistenceExceptions.toPersistenceException(e);
|
||||||
} finally {
|
|
||||||
_factory.unlock();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,7 +119,8 @@ public class EntityManagerImpl
|
||||||
_broker.lock();
|
_broker.lock();
|
||||||
try {
|
try {
|
||||||
if (_fetch == null)
|
if (_fetch == null)
|
||||||
_fetch = _emf.toFetchPlan(_broker.getFetchConfiguration());
|
_fetch = _emf.toFetchPlan(_broker,
|
||||||
|
_broker.getFetchConfiguration());
|
||||||
return _fetch;
|
return _fetch;
|
||||||
} finally {
|
} finally {
|
||||||
_broker.unlock();
|
_broker.unlock();
|
||||||
|
|
|
@ -79,8 +79,8 @@ public class Extent<T>
|
||||||
try {
|
try {
|
||||||
if (_fetch == null)
|
if (_fetch == null)
|
||||||
_fetch = ((EntityManagerFactoryImpl) _em.
|
_fetch = ((EntityManagerFactoryImpl) _em.
|
||||||
getEntityManagerFactory()).toFetchPlan(_extent.
|
getEntityManagerFactory()).toFetchPlan(_extent.getBroker(),
|
||||||
getFetchConfiguration());
|
_extent.getFetchConfiguration());
|
||||||
return _fetch;
|
return _fetch;
|
||||||
} finally {
|
} finally {
|
||||||
_extent.unlock();
|
_extent.unlock();
|
||||||
|
|
|
@ -83,7 +83,7 @@ public class FetchPlan {
|
||||||
/**
|
/**
|
||||||
* The maximum fetch depth when loading an object.
|
* The maximum fetch depth when loading an object.
|
||||||
*/
|
*/
|
||||||
public int getMaxFetchDepth(int depth) {
|
public int getMaxFetchDepth() {
|
||||||
return _fetch.getMaxFetchDepth();
|
return _fetch.getMaxFetchDepth();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -99,8 +99,8 @@ public class QueryImpl
|
||||||
try {
|
try {
|
||||||
if (_fetch == null)
|
if (_fetch == null)
|
||||||
_fetch = ((EntityManagerFactoryImpl) _em.
|
_fetch = ((EntityManagerFactoryImpl) _em.
|
||||||
getEntityManagerFactory()).toFetchPlan(_query.
|
getEntityManagerFactory()).toFetchPlan(_query.getBroker(),
|
||||||
getFetchConfiguration());
|
_query.getFetchConfiguration());
|
||||||
return _fetch;
|
return _fetch;
|
||||||
} finally {
|
} finally {
|
||||||
_query.unlock();
|
_query.unlock();
|
||||||
|
|
Loading…
Reference in New Issue