OPENJPA-1163:

Add configuration option that allows the elements in a persistent map to be treated as the owners of the relationship (ie updates to the hashmap can add elements without removing old ones). 
Submitted By : Ravi Palacherla

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@800563 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2009-08-03 21:04:50 +00:00
parent b5e893b5a7
commit 5232b72794
13 changed files with 87 additions and 58 deletions

View File

@ -60,6 +60,7 @@ public class Compatibility {
private boolean _useJPA2DefaultOrderColumnName = true;
private boolean _copyOnDetach = false;
private boolean _privatePersistentProperties = false;
private boolean _autoOff = true;
/**
* Whether to require exact identity value types when creating object
@ -76,6 +77,22 @@ public class Compatibility {
public void setStrictIdentityValues(boolean strictVals) {
_strictIdValues = strictVals;
}
/**
* Whether to turn collection/map tracing off in case of more number of modifications.
* Defaults to true.
*/
public boolean getAutoOff() {
return _autoOff;
}
/**
* Whether to turn collection/map tracing off in case of more number of modifications.
* Defaults to true.
*/
public void setAutoOff(boolean autoOff) {
_autoOff = autoOff;
}
/**
* Whether to interpret quoted numbers in query strings as numbers.

View File

@ -743,7 +743,9 @@ public class DetachManager
newVal = _proxy.newCollectionProxy(fmd.getProxyType(),
fmd.getElement().getDeclaredType(),
fmd.getInitializer() instanceof Comparator ?
(Comparator) fmd.getInitializer() : null);
(Comparator) fmd.getInitializer() : null,
sm.getBroker().getConfiguration().
getCompatibilityInstance().getAutoOff());
((Collection) newVal).addAll((Collection) curVal);
} else
newVal = _proxy.copyCollection((Collection) curVal);
@ -761,7 +763,9 @@ public class DetachManager
fmd.getKey().getDeclaredType(),
fmd.getElement().getDeclaredType(),
fmd.getInitializer() instanceof Comparator ?
(Comparator) fmd.getInitializer() : null);
(Comparator) fmd.getInitializer() : null,
sm.getBroker().getConfiguration().
getCompatibilityInstance().getAutoOff());
((Map) newVal).putAll((Map) curVal);
} else
newVal = _proxy.copyMap((Map) curVal);

View File

@ -125,7 +125,9 @@ class SingleFieldManager
return false;
proxy = checkProxy();
if (proxy == null) {
proxy = getProxyManager().newCustomProxy(objval);
proxy = getProxyManager().newCustomProxy(objval,
_sm.getBroker().getConfiguration().
getCompatibilityInstance().getAutoOff());
ret = proxy != null;
}
break;

View File

@ -1813,12 +1813,14 @@ public class StateManagerImpl
case JavaTypes.COLLECTION:
return mgr.newCollectionProxy(fmd.getProxyType(),
fmd.getElement().getDeclaredType(),
init instanceof Comparator ? (Comparator) init : null);
init instanceof Comparator ? (Comparator) init : null,
_broker.getConfiguration().getCompatibilityInstance().getAutoOff());
case JavaTypes.MAP:
return mgr.newMapProxy(fmd.getProxyType(),
fmd.getKey().getDeclaredType(),
fmd.getElement().getDeclaredType(),
init instanceof Comparator ? (Comparator) init : null);
init instanceof Comparator ? (Comparator) init : null,
_broker.getConfiguration().getCompatibilityInstance().getAutoOff());
}
return null;
}

View File

@ -69,7 +69,7 @@ public abstract class AbstractLRSProxyCollection
*/
public AbstractLRSProxyCollection(Class elementType, boolean ordered) {
_elementType = elementType;
_ct = new CollectionChangeTrackerImpl(this, false, ordered);
_ct = new CollectionChangeTrackerImpl(this, false, ordered,false);
_ct.setAutoOff(false);
}

View File

@ -73,7 +73,7 @@ public abstract class AbstractLRSProxyMap<K,V>
public AbstractLRSProxyMap(Class<K> keyType, Class<V> valueType) {
_keyType = keyType;
_valueType = valueType;
_ct = new MapChangeTrackerImpl(this);
_ct = new MapChangeTrackerImpl(this,false);
_ct.setAutoOff(false);
}

View File

@ -43,10 +43,11 @@ public class CollectionChangeTrackerImpl
* @param order true if the collection is ordered, false otherwise
*/
public CollectionChangeTrackerImpl(Collection coll, boolean dups,
boolean order) {
boolean order,boolean autoOff) {
_coll = coll;
_dups = dups;
_order = order;
this.setAutoOff(autoOff);
}
/**

View File

@ -36,8 +36,9 @@ public class MapChangeTrackerImpl
/**
* Constructor; supply delegate map.
*/
public MapChangeTrackerImpl(Map map) {
public MapChangeTrackerImpl(Map map, boolean autoOff) {
_map = map;
this.setAutoOff(autoOff);
}
public boolean getTrackKeys() {

View File

@ -38,5 +38,5 @@ public interface ProxyCollection
* Create a new instance of this proxy type.
*/
public ProxyCollection newInstance(Class elementType, Comparator comp,
boolean trackChanges);
boolean trackChanges, boolean autoOff);
}

View File

@ -82,7 +82,7 @@ public interface ProxyManager {
* element type and will use the given comparator, if it is not null.
*/
public Proxy newCollectionProxy(Class type, Class elementType,
Comparator compare);
Comparator compare, boolean autoOff);
/**
* Return a new map of the same type as the given one
@ -96,7 +96,7 @@ public interface ProxyManager {
* keyType/valueType, and will use the given comparator, if it is not null.
*/
public Proxy newMapProxy(Class type, Class keyType, Class valueType,
Comparator compare);
Comparator compare, boolean autoOff);
/**
* Return a copy of the given object with the same information, or null if
@ -112,5 +112,5 @@ public interface ProxyManager {
*
* @since 0.2.5
*/
public Proxy newCustomProxy (Object obj);
public Proxy newCustomProxy (Object obj, boolean autoOff);
}

View File

@ -184,11 +184,11 @@ public class ProxyManagerImpl
}
public Proxy newCollectionProxy(Class type, Class elementType,
Comparator compare) {
Comparator compare, boolean autoOff) {
type = toProxyableCollectionType(type);
ProxyCollection proxy = getFactoryProxyCollection(type);
return proxy.newInstance((_assertType) ? elementType : null, compare,
_trackChanges);
_trackChanges, autoOff);
}
public Map copyMap(Map orig) {
@ -202,11 +202,11 @@ public class ProxyManagerImpl
}
public Proxy newMapProxy(Class type, Class keyType,
Class elementType, Comparator compare) {
Class elementType, Comparator compare,boolean autoOff) {
type = toProxyableMapType(type);
ProxyMap proxy = getFactoryProxyMap(type);
return proxy.newInstance((_assertType) ? keyType : null,
(_assertType) ? elementType : null, compare, _trackChanges);
(_assertType) ? elementType : null, compare, _trackChanges, autoOff);
}
public Date copyDate(Date orig) {
@ -263,7 +263,7 @@ public class ProxyManagerImpl
return (proxy == null) ? null : proxy.copy(orig);
}
public Proxy newCustomProxy(Object orig) {
public Proxy newCustomProxy(Object orig, boolean autoOff) {
if (orig == null)
return null;
if (orig instanceof Proxy)
@ -274,14 +274,14 @@ public class ProxyManagerImpl
Comparator comp = (orig instanceof SortedSet)
? ((SortedSet) orig).comparator() : null;
Collection c = (Collection) newCollectionProxy(orig.getClass(),
null, comp);
null, comp, autoOff);
c.addAll((Collection) orig);
return (Proxy) c;
}
if (orig instanceof Map) {
Comparator comp = (orig instanceof SortedMap)
? ((SortedMap) orig).comparator() : null;
Map m = (Map) newMapProxy(orig.getClass(), null, null, comp);
Map m = (Map) newMapProxy(orig.getClass(), null, null, comp, autoOff);
m.putAll((Map) orig);
return (Proxy) m;
}
@ -814,7 +814,7 @@ public class ProxyManagerImpl
// new instance factory
m = bc.declareMethod("newInstance", ProxyCollection.class,
new Class[] { Class.class, Comparator.class, boolean.class });
new Class[] { Class.class, Comparator.class, boolean.class, boolean.class });
m.makePublic();
code = m.getCode(true);
@ -842,9 +842,10 @@ public class ProxyManagerImpl
code.aload().setLocal(ret);
code.constant().setValue(allowsDuplicates(type));
code.constant().setValue(isOrdered(type));
code.aload().setParam(3);
code.invokespecial().setMethod(CollectionChangeTrackerImpl.class,
"<init>", void.class, new Class[] { Collection.class,
boolean.class, boolean.class });
boolean.class, boolean.class, boolean.class });
code.putfield().setField(changeTracker);
ifins.setTarget(code.aload().setLocal(ret));
@ -948,7 +949,7 @@ public class ProxyManagerImpl
// new instance factory
m = bc.declareMethod("newInstance", ProxyMap.class,
new Class[] { Class.class, Class.class, Comparator.class,
boolean.class });
boolean.class,boolean.class });
m.makePublic();
code = m.getCode(true);
@ -977,8 +978,9 @@ public class ProxyManagerImpl
code.anew().setType(MapChangeTrackerImpl.class);
code.dup();
code.aload().setLocal(ret);
code.aload().setParam(4);
code.invokespecial().setMethod(MapChangeTrackerImpl.class,
"<init>", void.class, new Class[] { Map.class });
"<init>", void.class, new Class[] { Map.class, boolean.class });
code.putfield().setField(changeTracker);
ifins.setTarget(code.aload().setLocal(ret));

View File

@ -43,5 +43,5 @@ public interface ProxyMap
* Create a new instance of this proxy type.
*/
public ProxyMap newInstance(Class keyType, Class valueType,
Comparator compare, boolean trackChanges);
Comparator compare, boolean trackChanges, boolean autoOff);
}

View File

@ -173,12 +173,12 @@ public class TestProxyManager
}
public void testCopyProxyCollection() {
List orig = (List) _mgr.newCollectionProxy(ArrayList.class, null, null);
List orig = (List) _mgr.newCollectionProxy(ArrayList.class, null, null,true);
populate(orig);
assertListsEqual(new ArrayList(orig), (List) _mgr.copyCollection(orig));
TreeSet torig = (TreeSet) _mgr.newCollectionProxy(TreeSet.class, null,
new CustomComparator());
new CustomComparator(),true);
assertTrue(torig.comparator() instanceof CustomComparator);
populate(torig);
assertSortedSetsEqual(new TreeSet(torig), (SortedSet)
@ -189,7 +189,7 @@ public class TestProxyManager
// List doesn't support clone()
TreeSet torig = (TreeSet) _mgr.newCollectionProxy(TreeSet.class, null,
new CustomComparator());
new CustomComparator(),true);
assertTrue(torig.comparator() instanceof CustomComparator);
populate(torig);
assertSortedSetsEquals(new TreeSet(torig), (SortedSet) torig.clone());
@ -197,11 +197,11 @@ public class TestProxyManager
public void testListMethodsProxied()
throws Exception {
Class proxy = _mgr.newCollectionProxy(ArrayList.class, null, null).
Class proxy = _mgr.newCollectionProxy(ArrayList.class, null, null,true).
getClass();
assertListMethodsProxied(proxy);
proxy = _mgr.newCollectionProxy(CustomList.class, null, null).
proxy = _mgr.newCollectionProxy(CustomList.class, null, null,true).
getClass();
assertListMethodsProxied(proxy);
}
@ -255,19 +255,19 @@ public class TestProxyManager
public void testSetMethodsProxied()
throws Exception {
Class proxy = _mgr.newCollectionProxy(HashSet.class, null, null).
Class proxy = _mgr.newCollectionProxy(HashSet.class, null, null,true).
getClass();
assertCollectionMethodsProxied(proxy);
proxy = _mgr.newCollectionProxy(CustomSet.class, null, null).getClass();
proxy = _mgr.newCollectionProxy(CustomSet.class, null, null,true).getClass();
assertCollectionMethodsProxied(proxy);
proxy = _mgr.newCollectionProxy(CustomSortedSet.class, null, null).
proxy = _mgr.newCollectionProxy(CustomSortedSet.class, null, null,true).
getClass();
assertCollectionMethodsProxied(proxy);
proxy = _mgr.newCollectionProxy(CustomComparatorSortedSet.class, null,
new CustomComparator()).getClass();
new CustomComparator(),true).getClass();
assertCollectionMethodsProxied(proxy);
}
@ -277,7 +277,7 @@ public class TestProxyManager
if (queue == null)
return;
Class proxy = _mgr.newCollectionProxy(LinkedList.class, null, null).
Class proxy = _mgr.newCollectionProxy(LinkedList.class, null, null,true).
getClass();
assertTrue(queue.isAssignableFrom(proxy));
assertCollectionMethodsProxied(proxy);
@ -295,7 +295,7 @@ public class TestProxyManager
public void testLinkedListMethodsProxied()
throws Exception {
Class proxy = _mgr.newCollectionProxy(LinkedList.class, null, null).
Class proxy = _mgr.newCollectionProxy(LinkedList.class, null, null,true).
getClass();
assertListMethodsProxied(proxy);
assertNotNull(proxy.getDeclaredMethod("addFirst",
@ -308,7 +308,7 @@ public class TestProxyManager
public void testVectorMethodsProxied()
throws Exception {
Class proxy = _mgr.newCollectionProxy(Vector.class, null, null).
Class proxy = _mgr.newCollectionProxy(Vector.class, null, null,true).
getClass();
assertListMethodsProxied(proxy);
assertNotNull(proxy.getDeclaredMethod("addElement",
@ -326,7 +326,7 @@ public class TestProxyManager
}
public void testListChangeTracker() {
Proxy coll = _mgr.newCollectionProxy(ArrayList.class, null, null);
Proxy coll = _mgr.newCollectionProxy(ArrayList.class, null, null,true);
assertNotNull(coll);
assertNotNull(coll.getChangeTracker());
assertTrue(coll.getChangeTracker()
@ -338,7 +338,7 @@ public class TestProxyManager
}
public void testSetChangeTracker() {
Proxy coll = _mgr.newCollectionProxy(HashSet.class, null, null);
Proxy coll = _mgr.newCollectionProxy(HashSet.class, null, null,true);
assertNotNull(coll);
assertNotNull(coll.getChangeTracker());
assertTrue(coll.getChangeTracker()
@ -350,25 +350,25 @@ public class TestProxyManager
}
public void testCollectionInterfaceProxy() {
Proxy coll = _mgr.newCollectionProxy(Collection.class, null, null);
Proxy coll = _mgr.newCollectionProxy(Collection.class, null, null,true);
assertNotNull(coll);
}
public void testListInterfaceProxy() {
Proxy coll = _mgr.newCollectionProxy(List.class, null, null);
Proxy coll = _mgr.newCollectionProxy(List.class, null, null,true);
assertNotNull(coll);
assertTrue(coll instanceof List);
}
public void testSetInterfaceProxy() {
Proxy coll = _mgr.newCollectionProxy(Set.class, null, null);
Proxy coll = _mgr.newCollectionProxy(Set.class, null, null,true);
assertNotNull(coll);
assertTrue(coll instanceof Set);
assertFalse(coll instanceof SortedSet);
}
public void testSortedSetInterfaceProxy() {
Proxy coll = _mgr.newCollectionProxy(SortedSet.class, null, null);
Proxy coll = _mgr.newCollectionProxy(SortedSet.class, null, null,true);
assertNotNull(coll);
assertTrue(coll instanceof SortedSet);
}
@ -378,7 +378,7 @@ public class TestProxyManager
if (queue == null)
return;
Proxy coll = _mgr.newCollectionProxy(queue, null, null);
Proxy coll = _mgr.newCollectionProxy(queue, null, null,true);
assertNotNull(coll);
assertTrue(queue.isInstance(coll));
}
@ -479,12 +479,12 @@ public class TestProxyManager
}
public void testCopyProxyMap() {
Map orig = (Map) _mgr.newMapProxy(HashMap.class, null, null, null);
Map orig = (Map) _mgr.newMapProxy(HashMap.class, null, null, null,true);
populate(orig);
assertMapsEqual(new HashMap(orig), (Map) _mgr.copyMap(orig));
TreeMap torig = (TreeMap) _mgr.newMapProxy(TreeMap.class, null, null,
new CustomComparator());
new CustomComparator(),true);
assertTrue(torig.comparator() instanceof CustomComparator);
populate(torig);
assertSortedMapsEqual(new TreeMap(torig), (SortedMap)
@ -495,7 +495,7 @@ public class TestProxyManager
// Map does not support clone()
TreeMap torig = (TreeMap) _mgr.newMapProxy(TreeMap.class, null, null,
new CustomComparator());
new CustomComparator(),true);
assertTrue(torig.comparator() instanceof CustomComparator);
populate(torig);
assertSortedMapsEquals(new TreeMap(torig), (SortedMap) torig.clone());
@ -503,26 +503,26 @@ public class TestProxyManager
public void testMapMethodsProxied()
throws Exception {
Class proxy = _mgr.newMapProxy(HashMap.class, null, null, null).
Class proxy = _mgr.newMapProxy(HashMap.class, null, null, null,true).
getClass();
assertMapMethodsProxied(proxy);
proxy = _mgr.newMapProxy(TreeMap.class, null, null, null).getClass();
proxy = _mgr.newMapProxy(TreeMap.class, null, null, null,true).getClass();
assertMapMethodsProxied(proxy);
proxy = _mgr.newMapProxy(TreeMap.class, null, null,
new CustomComparator()).getClass();
new CustomComparator(),true).getClass();
assertMapMethodsProxied(proxy);
proxy = _mgr.newMapProxy(CustomMap.class, null, null, null).getClass();
proxy = _mgr.newMapProxy(CustomMap.class, null, null, null,true).getClass();
assertMapMethodsProxied(proxy);
proxy = _mgr.newMapProxy(CustomSortedMap.class, null, null, null).
proxy = _mgr.newMapProxy(CustomSortedMap.class, null, null, null,true).
getClass();
assertMapMethodsProxied(proxy);
proxy = _mgr.newMapProxy(CustomComparatorSortedMap.class, null, null,
new CustomComparator()).getClass();
new CustomComparator(),true).getClass();
assertMapMethodsProxied(proxy);
}
@ -554,7 +554,7 @@ public class TestProxyManager
public void testPropertiesMethodsProxied()
throws Exception {
Class proxy = _mgr.newMapProxy(Properties.class, null, null, null).
Class proxy = _mgr.newMapProxy(Properties.class, null, null, null,true).
getClass();
assertMapMethodsProxied(proxy);
assertNotNull(proxy.getDeclaredMethod("setProperty",
@ -797,7 +797,7 @@ public class TestProxyManager
NonproxyableBean orig = new NonproxyableBean(1);
populate(orig);
assertNull(_mgr.copyCustom(orig));
assertNull(_mgr.newCustomProxy(orig));
assertNull(_mgr.newCustomProxy(orig,true));
}
@ -815,7 +815,7 @@ public class TestProxyManager
}
public void testCopyProxyBean() {
CustomBean orig = (CustomBean) _mgr.newCustomProxy(new CustomBean());
CustomBean orig = (CustomBean) _mgr.newCustomProxy(new CustomBean(),true);
populate(orig);
CustomBean comp = new CustomBean();
populate(comp);
@ -824,11 +824,11 @@ public class TestProxyManager
public void testBeanMethodsProxied()
throws Exception {
Class proxy = _mgr.newCustomProxy(new CustomBean()).getClass();
Class proxy = _mgr.newCustomProxy(new CustomBean(),true).getClass();
assertBeanMethodsProxied(proxy);
proxy = _mgr.newCustomProxy(new CustomCopyConstructorBean
(new CustomBean())).getClass();
(new CustomBean()),true).getClass();
assertBeanMethodsProxied(proxy);
}