OPENJPA-2165 Minor fixes, more proxy types, testcases, and doc udpates

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1331051 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jeremy Bauer 2012-04-26 19:53:49 +00:00
parent aac116c904
commit df8d3c4416
69 changed files with 9211 additions and 341 deletions

View File

@ -1362,6 +1362,6 @@ public class FieldMapping
@Override
public boolean isDelayCapable() {
return (getOrderColumn() == null && super.isDelayCapable());
return (getOrderColumn() == null && !isInDefaultFetchGroup() && super.isDelayCapable());
}
}

View File

@ -29,410 +29,418 @@ import java.util.ListIterator;
import org.apache.openjpa.kernel.AutoDetach;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.kernel.BrokerFactory;
import org.apache.openjpa.kernel.DetachedStateManager;
import org.apache.openjpa.kernel.OpenJPAStateManager;
public class DelayedArrayListProxy extends ArrayList
implements ProxyCollection, DelayedProxy
{
private transient OpenJPAStateManager sm;
private transient OpenJPAStateManager _ownerSm;
private transient int field;
private transient CollectionChangeTracker changeTracker;
private transient Class<?> elementType;
private transient boolean _directAccess = false;
private transient BrokerFactory _brokerFactory = null;
private transient Broker _broker = null;
private transient OpenJPAStateManager _delayedSm;
private transient int _delayedField;
private transient boolean _detached = false;
public DelayedArrayListProxy()
{
}
public DelayedArrayListProxy(Collection paramCollection)
{
super(paramCollection);
}
/**
* ArrayList proxy with delay loading capability. Allows non-indexed
* add and remove operations to occur on an unloaded collection. Operations
* that require a load will trigger a load.
*/
@SuppressWarnings({"rawtypes","unchecked"})
public class DelayedArrayListProxy extends ArrayList implements ProxyCollection, DelayedProxy {
public DelayedArrayListProxy(int paramInt)
{
super(paramInt);
}
private transient OpenJPAStateManager sm;
private transient int field;
private transient CollectionChangeTracker changeTracker;
private transient Class<?> elementType;
private transient OpenJPAStateManager _ownerSm;
private transient boolean _directAccess = false;
private transient BrokerFactory _brokerFactory = null;
private transient Broker _broker = null;
private transient OpenJPAStateManager _delayedSm;
private transient int _delayedField;
private transient boolean _detached = false;
public void setOwner(OpenJPAStateManager paramOpenJPAStateManager, int paramInt)
{
// If clearing the owner of this proxy, store away what is necessary for
// delayed loading
if (paramOpenJPAStateManager == null && paramInt == -1 && sm != null) {
_detached = true;
_delayedSm = sm;
_delayedField = field;
} else {
_detached = false;
public DelayedArrayListProxy() {
}
this.sm = paramOpenJPAStateManager;
if (sm != null && sm.getPersistenceCapable() != null) {
_ownerSm = (OpenJPAStateManager) sm.getPersistenceCapable().pcGetStateManager();
public DelayedArrayListProxy(Collection paramCollection) {
super(paramCollection);
}
this.field = paramInt;
if (sm != null && sm.getContext() != null) {
_brokerFactory = sm.getContext().getBroker().getBrokerFactory();
public DelayedArrayListProxy(int paramInt) {
super(paramInt);
}
}
public int getDelayedField() {
if (field == -1) {
return _delayedField;
}
return field;
}
public OpenJPAStateManager getDelayedOwner() {
if (sm == null) {
return _delayedSm;
}
return sm;
}
public OpenJPAStateManager getOwner()
{
return sm;
}
public void setOwner(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
// If clearing the owner of this proxy, store away what is necessary for
// delayed loading
if (sm != null && detaching(paramOpenJPAStateManager, paramInt)) {
_detached = true;
_delayedSm = sm;
_delayedField = field;
} else {
_detached = false;
}
public int getOwnerField()
{
return field;
}
public Object clone()
{
if (isDirectAccess()) {
return super.clone();
this.sm = paramOpenJPAStateManager;
if (sm != null && sm.getPersistenceCapable() != null) {
_ownerSm = (OpenJPAStateManager) sm.getPersistenceCapable()
.pcGetStateManager();
}
this.field = paramInt;
if (sm != null && sm.getContext() != null) {
_brokerFactory = sm.getContext().getBroker().getBrokerFactory();
}
}
if (isDelayLoad()) {
load();
private boolean detaching(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
if ((paramOpenJPAStateManager == null && paramInt == -1)
|| (paramOpenJPAStateManager != null && paramOpenJPAStateManager instanceof DetachedStateManager)) {
return true;
}
return false;
}
Proxy localProxy = (Proxy)super.clone();
localProxy.setOwner(null, 0);
return localProxy;
}
public ChangeTracker getChangeTracker()
{
return this.changeTracker;
}
protected void setChangeTracker(CollectionChangeTracker ct) {
changeTracker = ct;
}
public Object copy(Object paramObject)
{
return new ArrayList((Collection)paramObject);
}
public Class getElementType()
{
return this.elementType;
}
protected void setElementType(Class<?> elemType) {
elementType = elemType;
}
@Override
public ProxyCollection newInstance(Class paramClass, Comparator paramComparator, boolean paramBoolean1,
boolean paramBoolean2)
{
DelayedArrayListProxy proxy = new DelayedArrayListProxy();
proxy.elementType = paramClass;
proxy.changeTracker = new DelayedCollectionChangeTrackerImpl(proxy, true, true, paramBoolean2);
return proxy;
}
public boolean add(Object paramObject)
{
if (_directAccess) {
return super.add(paramObject);
public int getDelayedField() {
if (field == -1 || _detached) {
return _delayedField;
}
return field;
}
ProxyCollections.beforeAdd(this, paramObject);
boolean bool = super.add(paramObject);
return ProxyCollections.afterAdd(this, paramObject, bool);
}
public void add(int paramInt, Object paramObject)
{
if (!_directAccess) {
public OpenJPAStateManager getDelayedOwner() {
if (sm == null || _detached) {
return _delayedSm;
}
return sm;
}
public OpenJPAStateManager getOwner() {
return sm;
}
public int getOwnerField() {
return field;
}
public Object clone() {
if (isDirectAccess()) {
return super.clone();
}
if (isDelayLoad()) {
load();
}
Proxy localProxy = (Proxy) super.clone();
localProxy.setOwner(null, 0);
return localProxy;
}
ProxyCollections.beforeAdd(this, paramInt, paramObject);
super.add(paramInt, paramObject);
}
public void clear()
{
if (!_directAccess) {
public ChangeTracker getChangeTracker() {
return this.changeTracker;
}
protected void setChangeTracker(CollectionChangeTracker ct) {
changeTracker = ct;
}
public Object copy(Object paramObject) {
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeClear(this);
return new ArrayList((Collection) paramObject);
}
super.clear();
}
public boolean addAll(int paramInt, Collection paramCollection)
{
if (isDelayLoad()) {
load();
public Class getElementType() {
return this.elementType;
}
return ProxyCollections.addAll(this, paramInt, paramCollection);
}
public boolean addAll(Collection paramCollection)
{
if (_directAccess) {
return super.addAll(paramCollection);
protected void setElementType(Class<?> elemType) {
elementType = elemType;
}
return ProxyCollections.addAll(this, paramCollection);
}
public boolean remove(Object paramObject)
{
if (_directAccess) {
return super.remove(paramObject);
}
ProxyCollections.beforeRemove(this, paramObject);
setDirectAccess(true);
boolean bool = super.remove(paramObject);
setDirectAccess(false);
return ProxyCollections.afterRemove(this, paramObject, bool);
}
public Object remove(int paramInt)
{
if (_directAccess) {
return super.remove(paramInt);
@Override
public ProxyCollection newInstance(Class paramClass,
Comparator paramComparator, boolean paramBoolean1,
boolean paramBoolean2) {
DelayedArrayListProxy proxy = new DelayedArrayListProxy();
proxy.elementType = paramClass;
proxy.changeTracker = new DelayedCollectionChangeTrackerImpl(proxy,
true, true, paramBoolean2);
return proxy;
}
if (isDelayLoad()) {
load();
public boolean add(Object paramObject) {
if (_directAccess) {
return super.add(paramObject);
}
ProxyCollections.beforeAdd(this, paramObject);
boolean bool = super.add(paramObject);
return ProxyCollections.afterAdd(this, paramObject, bool);
}
ProxyCollections.beforeRemove(this, paramInt);
Object localObject = super.remove(paramInt);
return ProxyCollections.afterRemove(this, paramInt, localObject);
}
public Object set(int paramInt, Object paramObject)
{
if (_directAccess) {
return super.set(paramInt, paramObject);
public void add(int paramInt, Object paramObject) {
if (!_directAccess) {
if (isDelayLoad()) {
load();
}
}
ProxyCollections.beforeAdd(this, paramInt, paramObject);
super.add(paramInt, paramObject);
}
if (isDelayLoad()) {
load();
public void clear() {
if (!_directAccess) {
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeClear(this);
}
super.clear();
}
ProxyCollections.beforeSet(this, paramInt, paramObject);
Object localObject = super.set(paramInt, paramObject);
return ProxyCollections.afterSet(this, paramInt, paramObject, localObject);
}
public Iterator iterator()
{
if (_directAccess) {
return super.iterator();
public boolean addAll(int paramInt, Collection paramCollection) {
if (isDelayLoad()) {
load();
}
return ProxyCollections.addAll(this, paramInt, paramCollection);
}
if (isDelayLoad()) {
load();
public boolean addAll(Collection paramCollection) {
if (_directAccess) {
return super.addAll(paramCollection);
}
return ProxyCollections.addAll(this, paramCollection);
}
Iterator localIterator = super.iterator();
return ProxyCollections.afterIterator(this, localIterator);
}
public ListIterator listIterator(int paramInt)
{
if (_directAccess) {
return super.listIterator(paramInt);
public boolean remove(Object paramObject) {
if (_directAccess) {
return super.remove(paramObject);
}
ProxyCollections.beforeRemove(this, paramObject);
setDirectAccess(true);
boolean bool = super.remove(paramObject);
setDirectAccess(false);
return ProxyCollections.afterRemove(this, paramObject, bool);
}
if (isDelayLoad()) {
load();
public Object remove(int paramInt) {
if (_directAccess) {
return super.remove(paramInt);
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeRemove(this, paramInt);
Object localObject = super.remove(paramInt);
return ProxyCollections.afterRemove(this, paramInt, localObject);
}
ListIterator localListIterator = super.listIterator(paramInt);
return ProxyCollections.afterListIterator(this, paramInt, localListIterator);
}
public ListIterator listIterator()
{
if (_directAccess) {
return super.listIterator();
public Object set(int paramInt, Object paramObject) {
if (_directAccess) {
return super.set(paramInt, paramObject);
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeSet(this, paramInt, paramObject);
Object localObject = super.set(paramInt, paramObject);
return ProxyCollections.afterSet(this, paramInt, paramObject,
localObject);
}
if (isDelayLoad()) {
load();
public Iterator iterator() {
if (_directAccess) {
return super.iterator();
}
if (isDelayLoad()) {
load();
}
Iterator localIterator = super.iterator();
return ProxyCollections.afterIterator(this, localIterator);
}
ListIterator localListIterator = super.listIterator();
return ProxyCollections.afterListIterator(this, localListIterator);
}
public boolean removeAll(Collection paramCollection)
{
if (_directAccess) {
return super.removeAll(paramCollection);
public ListIterator listIterator(int paramInt) {
if (_directAccess) {
return super.listIterator(paramInt);
}
if (isDelayLoad()) {
load();
}
ListIterator localListIterator = super.listIterator(paramInt);
return ProxyCollections.afterListIterator(this, paramInt,
localListIterator);
}
if (isDelayLoad()) {
load();
public ListIterator listIterator() {
if (_directAccess) {
return super.listIterator();
}
if (isDelayLoad()) {
load();
}
ListIterator localListIterator = super.listIterator();
return ProxyCollections.afterListIterator(this, localListIterator);
}
return ProxyCollections.removeAll(this, paramCollection);
}
public boolean retainAll(Collection paramCollection)
{
if (_directAccess) {
return super.retainAll(paramCollection);
public boolean removeAll(Collection paramCollection) {
if (_directAccess) {
return super.removeAll(paramCollection);
}
return ProxyCollections.removeAll(this, paramCollection);
}
if (isDelayLoad()) {
load();
public boolean retainAll(Collection paramCollection) {
if (_directAccess) {
return super.retainAll(paramCollection);
}
if (isDelayLoad()) {
load();
}
return ProxyCollections.retainAll(this, paramCollection);
}
return ProxyCollections.retainAll(this, paramCollection);
}
protected Object writeReplace()
throws ObjectStreamException
{
if (isDelayLoad()) {
load();
protected Object writeReplace() throws ObjectStreamException {
if (isDelayLoad()) {
load();
}
return Proxies.writeReplace(this, true);
}
return Proxies.writeReplace(this, true);
}
public boolean isDelayLoad() {
return ProxyCollections.isDelayed(this);
public boolean isDelayLoad() {
return ProxyCollections.isDelayed(this);
}
@Override
public Object get(int location) {
if (!_directAccess && isDelayLoad()) {
load();
@Override
public Object get(int location) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.get(location);
}
return super.get(location);
}
@Override
public int indexOf(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
@Override
public int indexOf(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.indexOf(object);
}
return super.indexOf(object);
}
@Override
public int lastIndexOf(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.lastIndexOf(object);
}
@Override
public int lastIndexOf(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.lastIndexOf(object);
}
@Override
public List subList(int start, int end) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.subList(start, end);
}
@Override
public List subList(int start, int end) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.subList(start, end);
}
@Override
public boolean contains(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.contains(object);
}
@Override
public boolean contains(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.contains(object);
}
@Override
public boolean containsAll(Collection collection) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.containsAll(collection);
}
@Override
public boolean containsAll(Collection collection) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.containsAll(collection);
}
@Override
public boolean isEmpty() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.isEmpty();
}
@Override
public boolean isEmpty() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.isEmpty();
}
@Override
public int size() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.size();
}
@Override
public int size() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.size();
}
@Override
public Object[] toArray() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray();
}
@Override
public Object[] toArray() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray();
}
@Override
public Object[] toArray(Object[] array) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray(array);
}
@Override
public Object[] toArray(Object[] array) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray(array);
}
public boolean isDirectAccess() {
return _directAccess;
}
public void setDirectAccess(boolean direct) {
_directAccess = direct;
}
public BrokerFactory getBrokerFactory() {
return _brokerFactory;
}
public boolean equals(Object paramObject) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.equals(paramObject);
}
@Override
public void load() {
ProxyCollections.loadCollection(this);
}
public int hashCode() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.hashCode();
}
@Override
public Broker getBroker() {
if (_broker == null || _broker.isClosed()) {
if (_brokerFactory != null) {
_broker = _brokerFactory.newBroker();
public boolean isDirectAccess() {
return _directAccess;
}
public void setDirectAccess(boolean direct) {
_directAccess = direct;
}
public BrokerFactory getBrokerFactory() {
return _brokerFactory;
}
@Override
public void load() {
ProxyCollections.loadCollection(this);
}
@Override
public Broker getBroker() {
if (_broker == null || _broker.isClosed()) {
if (_brokerFactory != null) {
_broker = _brokerFactory.newBroker();
}
}
return _broker;
}
@Override
public void closeBroker() {
if (_broker != null && !_broker.isClosed()) {
_broker.setAutoDetach(AutoDetach.DETACH_CLOSE);
_broker.close();
_broker = null;
}
}
return _broker;
}
@Override
public void closeBroker() {
if (_broker != null && !_broker.isClosed()) {
_broker.setAutoDetach(AutoDetach.DETACH_CLOSE);
_broker.close();
_broker = null;
}
}
@Override
public OpenJPAStateManager getOwnerStateManager() {
return _ownerSm;
}
@Override
public boolean isDetached() {
return _detached;
}
@Override
public OpenJPAStateManager getOwnerStateManager() {
return _ownerSm;
}
@Override
public boolean isDetached() {
return _detached;
}
}

View File

@ -0,0 +1,361 @@
/*
* 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.util;
import java.io.ObjectStreamException;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.openjpa.kernel.AutoDetach;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.kernel.BrokerFactory;
import org.apache.openjpa.kernel.DetachedStateManager;
import org.apache.openjpa.kernel.OpenJPAStateManager;
/**
* HashSet proxy with delay loading capability. Allows non-indexed add and
* remove operations to occur on an unloaded collection. Operations that require
* a load will trigger a load.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public class DelayedHashSetProxy extends HashSet implements DelayedProxy, ProxyCollection {
private transient OpenJPAStateManager sm;
private transient int field;
private transient CollectionChangeTracker changeTracker;
private transient Class<?> elementType;
private transient OpenJPAStateManager _ownerSm;
private transient boolean _directAccess = false;
private transient BrokerFactory _brokerFactory = null;
private transient Broker _broker = null;
private transient OpenJPAStateManager _delayedSm;
private transient int _delayedField;
private transient boolean _detached = false;
public DelayedHashSetProxy(Collection<?> paramCollection) {
super(paramCollection);
}
public DelayedHashSetProxy(int paramInt, float paramFloat) {
super(paramInt, paramFloat);
}
public DelayedHashSetProxy(int paramInt) {
super(paramInt);
}
public DelayedHashSetProxy() {
}
public void setOwner(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
// If clearing the owner of this proxy, store away what is necessary for
// delayed loading
if (sm != null && detaching(paramOpenJPAStateManager, paramInt)) {
_detached = true;
_delayedSm = sm;
_delayedField = field;
} else {
_detached = false;
}
this.sm = paramOpenJPAStateManager;
if (sm != null && sm.getPersistenceCapable() != null) {
_ownerSm = (OpenJPAStateManager) sm.getPersistenceCapable()
.pcGetStateManager();
}
this.field = paramInt;
if (sm != null && sm.getContext() != null) {
_brokerFactory = sm.getContext().getBroker().getBrokerFactory();
}
}
private boolean detaching(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
if ((paramOpenJPAStateManager == null && paramInt == -1)
|| (paramOpenJPAStateManager != null && paramOpenJPAStateManager instanceof DetachedStateManager)) {
return true;
}
return false;
}
public OpenJPAStateManager getOwner() {
return this.sm;
}
public int getOwnerField() {
return this.field;
}
public ChangeTracker getChangeTracker() {
return this.changeTracker;
}
@Override
public Object copy(Object paramObject) {
return new HashSet((Collection) paramObject);
}
public Class getElementType() {
return this.elementType;
}
public ProxyCollection newInstance(Class paramClass,
Comparator paramComparator, boolean paramBoolean1,
boolean paramBoolean2) {
DelayedHashSetProxy localproxy = new DelayedHashSetProxy();
localproxy.elementType = paramClass;
if (paramBoolean1)
localproxy.changeTracker = new DelayedCollectionChangeTrackerImpl(
localproxy, false, false, paramBoolean2);
return localproxy;
}
@Override
public Object clone() {
if (isDirectAccess()) {
return super.clone();
}
if (isDelayLoad()) {
load();
}
Proxy localProxy = (Proxy) super.clone();
localProxy.setOwner(null, 0);
return localProxy;
}
@Override
public boolean add(Object paramObject) {
if (_directAccess) {
return super.add(paramObject);
}
ProxyCollections.beforeAdd(this, paramObject);
boolean bool = super.add(paramObject);
return ProxyCollections.afterAdd(this, paramObject, bool);
}
@Override
public void clear() {
if (!_directAccess) {
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeClear(this);
}
super.clear();
}
@Override
public Iterator iterator() {
if (_directAccess) {
return super.iterator();
}
if (isDelayLoad()) {
load();
}
Iterator localIterator = super.iterator();
return ProxyCollections.afterIterator(this, localIterator);
}
@Override
public boolean remove(Object paramObject) {
if (_directAccess) {
return super.remove(paramObject);
}
ProxyCollections.beforeRemove(this, paramObject);
setDirectAccess(true);
boolean bool = super.remove(paramObject);
setDirectAccess(false);
return ProxyCollections.afterRemove(this, paramObject, bool);
}
@Override
public boolean removeAll(Collection paramCollection) {
if (_directAccess) {
return super.removeAll(paramCollection);
}
return ProxyCollections.removeAll(this, paramCollection);
}
@Override
public boolean addAll(Collection paramCollection) {
if (_directAccess) {
return super.addAll(paramCollection);
}
return ProxyCollections.addAll(this, paramCollection);
}
@Override
public boolean retainAll(Collection paramCollection) {
if (_directAccess) {
return super.retainAll(paramCollection);
}
if (isDelayLoad()) {
load();
}
return ProxyCollections.retainAll(this, paramCollection);
}
protected Object writeReplace() throws ObjectStreamException {
if (isDelayLoad()) {
load();
}
return Proxies.writeReplace(this, true);
}
@Override
public int size() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.size();
}
@Override
public boolean isEmpty() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.isEmpty();
}
@Override
public boolean contains(Object o) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.contains(o);
}
@Override
public Object[] toArray() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray();
}
@Override
public Object[] toArray(Object[] a) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray(a);
}
@Override
public boolean containsAll(Collection c) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.containsAll(c);
}
@Override
public String toString() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toString();
}
public boolean equals(Object paramObject) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.equals(paramObject);
}
public int hashCode() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.hashCode();
}
// //////////////////////////////////////
// DelayedProxy methods
// //////////////////////////////////////
public int getDelayedField() {
if (field == -1 || _detached) {
return _delayedField;
}
return field;
}
public OpenJPAStateManager getDelayedOwner() {
if (sm == null || _detached) {
return _delayedSm;
}
return sm;
}
public boolean isDirectAccess() {
return _directAccess;
}
public void setDirectAccess(boolean direct) {
_directAccess = direct;
}
public BrokerFactory getBrokerFactory() {
return _brokerFactory;
}
@Override
public void load() {
ProxyCollections.loadCollection(this);
}
@Override
public Broker getBroker() {
if (_broker == null || _broker.isClosed()) {
if (_brokerFactory != null) {
_broker = _brokerFactory.newBroker();
}
}
return _broker;
}
@Override
public void closeBroker() {
if (_broker != null && !_broker.isClosed()) {
_broker.setAutoDetach(AutoDetach.DETACH_CLOSE);
_broker.close();
_broker = null;
}
}
@Override
public OpenJPAStateManager getOwnerStateManager() {
return _ownerSm;
}
@Override
public boolean isDetached() {
return _detached;
}
public boolean isDelayLoad() {
return ProxyCollections.isDelayed(this);
}
}

View File

@ -0,0 +1,361 @@
/*
* 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.util;
import java.io.ObjectStreamException;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import org.apache.openjpa.kernel.AutoDetach;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.kernel.BrokerFactory;
import org.apache.openjpa.kernel.DetachedStateManager;
import org.apache.openjpa.kernel.OpenJPAStateManager;
/**
* LinkedHashSet proxy with delay loading capability. Allows non-indexed
* add and remove operations to occur on an unloaded collection. Operations
* that require a load will trigger a load.
*/
@SuppressWarnings({"rawtypes","unchecked"})
public class DelayedLinkedHashSetProxy extends LinkedHashSet implements DelayedProxy, ProxyCollection {
private transient OpenJPAStateManager sm;
private transient int field;
private transient CollectionChangeTracker changeTracker;
private transient Class<?> elementType;
private transient OpenJPAStateManager _ownerSm;
private transient boolean _directAccess = false;
private transient BrokerFactory _brokerFactory = null;
private transient Broker _broker = null;
private transient OpenJPAStateManager _delayedSm;
private transient int _delayedField;
private transient boolean _detached = false;
public DelayedLinkedHashSetProxy(Collection<?> paramCollection) {
super(paramCollection);
}
public DelayedLinkedHashSetProxy(int paramInt, float paramFloat) {
super(paramInt, paramFloat);
}
public DelayedLinkedHashSetProxy(int paramInt) {
super(paramInt);
}
public DelayedLinkedHashSetProxy() {
}
public void setOwner(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
// If clearing the owner of this proxy, store away what is necessary for
// delayed loading
if (sm != null && detaching(paramOpenJPAStateManager, paramInt)) {
_detached = true;
_delayedSm = sm;
_delayedField = field;
} else {
_detached = false;
}
this.sm = paramOpenJPAStateManager;
if (sm != null && sm.getPersistenceCapable() != null) {
_ownerSm = (OpenJPAStateManager) sm.getPersistenceCapable()
.pcGetStateManager();
}
this.field = paramInt;
if (sm != null && sm.getContext() != null) {
_brokerFactory = sm.getContext().getBroker().getBrokerFactory();
}
}
private boolean detaching(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
if ((paramOpenJPAStateManager == null && paramInt == -1)
|| (paramOpenJPAStateManager != null && paramOpenJPAStateManager instanceof DetachedStateManager)) {
return true;
}
return false;
}
public OpenJPAStateManager getOwner() {
return this.sm;
}
public int getOwnerField() {
return this.field;
}
public ChangeTracker getChangeTracker() {
return this.changeTracker;
}
@Override
public Object copy(Object paramObject) {
return new LinkedHashSet((Collection) paramObject);
}
public Class getElementType() {
return this.elementType;
}
public ProxyCollection newInstance(Class paramClass,
Comparator paramComparator, boolean paramBoolean1,
boolean paramBoolean2) {
DelayedLinkedHashSetProxy localproxy = new DelayedLinkedHashSetProxy();
localproxy.elementType = paramClass;
if (paramBoolean1)
localproxy.changeTracker = new DelayedCollectionChangeTrackerImpl(
localproxy, false, false, paramBoolean2);
return localproxy;
}
@Override
public Object clone() {
if (isDirectAccess()) {
return super.clone();
}
if (isDelayLoad()) {
load();
}
Proxy localProxy = (Proxy) super.clone();
localProxy.setOwner(null, 0);
return localProxy;
}
@Override
public boolean add(Object paramObject) {
if (_directAccess) {
return super.add(paramObject);
}
ProxyCollections.beforeAdd(this, paramObject);
boolean bool = super.add(paramObject);
return ProxyCollections.afterAdd(this, paramObject, bool);
}
@Override
public void clear() {
if (!_directAccess) {
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeClear(this);
}
super.clear();
}
@Override
public Iterator iterator() {
if (_directAccess) {
return super.iterator();
}
if (isDelayLoad()) {
load();
}
Iterator localIterator = super.iterator();
return ProxyCollections.afterIterator(this, localIterator);
}
@Override
public boolean remove(Object paramObject) {
if (_directAccess) {
return super.remove(paramObject);
}
ProxyCollections.beforeRemove(this, paramObject);
setDirectAccess(true);
boolean bool = super.remove(paramObject);
setDirectAccess(false);
return ProxyCollections.afterRemove(this, paramObject, bool);
}
@Override
public boolean removeAll(Collection paramCollection) {
if (_directAccess) {
return super.removeAll(paramCollection);
}
return ProxyCollections.removeAll(this, paramCollection);
}
@Override
public boolean addAll(Collection paramCollection) {
if (_directAccess) {
return super.addAll(paramCollection);
}
return ProxyCollections.addAll(this, paramCollection);
}
@Override
public boolean retainAll(Collection paramCollection) {
if (_directAccess) {
return super.retainAll(paramCollection);
}
if (isDelayLoad()) {
load();
}
return ProxyCollections.retainAll(this, paramCollection);
}
protected Object writeReplace() throws ObjectStreamException {
if (isDelayLoad()) {
load();
}
return Proxies.writeReplace(this, true);
}
@Override
public int size() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.size();
}
@Override
public boolean isEmpty() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.isEmpty();
}
@Override
public boolean contains(Object o) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.contains(o);
}
@Override
public Object[] toArray() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray();
}
@Override
public Object[] toArray(Object[] a) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray(a);
}
@Override
public boolean containsAll(Collection c) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.containsAll(c);
}
@Override
public String toString() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toString();
}
public boolean equals(Object paramObject) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.equals(paramObject);
}
public int hashCode() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.hashCode();
}
// //////////////////////////////////////
// DelayedProxy methods
// //////////////////////////////////////
public int getDelayedField() {
if (field == -1 || _detached) {
return _delayedField;
}
return field;
}
public OpenJPAStateManager getDelayedOwner() {
if (sm == null || _detached) {
return _delayedSm;
}
return sm;
}
public boolean isDirectAccess() {
return _directAccess;
}
public void setDirectAccess(boolean direct) {
_directAccess = direct;
}
public BrokerFactory getBrokerFactory() {
return _brokerFactory;
}
@Override
public void load() {
ProxyCollections.loadCollection(this);
}
@Override
public Broker getBroker() {
if (_broker == null || _broker.isClosed()) {
if (_brokerFactory != null) {
_broker = _brokerFactory.newBroker();
}
}
return _broker;
}
@Override
public void closeBroker() {
if (_broker != null && !_broker.isClosed()) {
_broker.setAutoDetach(AutoDetach.DETACH_CLOSE);
_broker.close();
_broker = null;
}
}
@Override
public OpenJPAStateManager getOwnerStateManager() {
return _ownerSm;
}
@Override
public boolean isDetached() {
return _detached;
}
protected boolean isDelayLoad() {
return ProxyCollections.isDelayed(this);
}
}

View File

@ -0,0 +1,701 @@
/*
* 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.util;
import java.io.ObjectStreamException;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import org.apache.openjpa.kernel.AutoDetach;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.kernel.BrokerFactory;
import org.apache.openjpa.kernel.DetachedStateManager;
import org.apache.openjpa.kernel.OpenJPAStateManager;
/**
* LinkedList proxy with delay loading capability. Allows non-indexed
* add and remove operations to occur on an unloaded collection. Operations
* that require a load will trigger a load.
*/
@SuppressWarnings({"rawtypes","unchecked"})
public class DelayedLinkedListProxy extends LinkedList implements ProxyCollection, DelayedProxy {
private transient OpenJPAStateManager sm;
private transient int field;
private transient CollectionChangeTracker changeTracker;
private transient Class elementType;
private transient OpenJPAStateManager _ownerSm;
private transient boolean _directAccess = false;
private transient BrokerFactory _brokerFactory = null;
private transient Broker _broker = null;
private transient OpenJPAStateManager _delayedSm;
private transient int _delayedField;
private transient boolean _detached = false;
public DelayedLinkedListProxy(Collection paramCollection) {
super(paramCollection);
}
public DelayedLinkedListProxy() {
}
@Override
public void setOwner(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
// If clearing the owner of this proxy, store away what is necessary for
// delayed loading
if (sm != null && detaching(paramOpenJPAStateManager, paramInt)) {
_detached = true;
_delayedSm = sm;
_delayedField = field;
} else {
_detached = false;
}
this.sm = paramOpenJPAStateManager;
if (sm != null && sm.getPersistenceCapable() != null) {
_ownerSm = (OpenJPAStateManager) sm.getPersistenceCapable()
.pcGetStateManager();
}
this.field = paramInt;
if (sm != null && sm.getContext() != null) {
_brokerFactory = sm.getContext().getBroker().getBrokerFactory();
}
}
private boolean detaching(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
if ((paramOpenJPAStateManager == null && paramInt == -1)
|| (paramOpenJPAStateManager != null && paramOpenJPAStateManager instanceof DetachedStateManager)) {
return true;
}
return false;
}
@Override
public OpenJPAStateManager getOwner() {
return this.sm;
}
@Override
public int getOwnerField() {
return this.field;
}
@Override
public ChangeTracker getChangeTracker() {
return this.changeTracker;
}
@Override
public Object copy(Object paramObject) {
return new LinkedList((Collection) paramObject);
}
@Override
public Class getElementType() {
return this.elementType;
}
@Override
public ProxyCollection newInstance(Class paramClass,
Comparator paramComparator, boolean paramBoolean1,
boolean paramBoolean2) {
DelayedLinkedListProxy localproxy = new DelayedLinkedListProxy();
localproxy.elementType = paramClass;
if (paramBoolean1)
localproxy.changeTracker = new DelayedCollectionChangeTrackerImpl(
localproxy, true, true, paramBoolean2);
return localproxy;
}
// //////////////////////////////////////
// DelayedProxy methods
// //////////////////////////////////////
@Override
public int getDelayedField() {
if (field == -1 || _detached) {
return _delayedField;
}
return field;
}
@Override
public OpenJPAStateManager getDelayedOwner() {
if (sm == null || _detached) {
return _delayedSm;
}
return sm;
}
@Override
public boolean isDirectAccess() {
return _directAccess;
}
@Override
public void setDirectAccess(boolean direct) {
_directAccess = direct;
}
public BrokerFactory getBrokerFactory() {
return _brokerFactory;
}
@Override
public void load() {
ProxyCollections.loadCollection(this);
}
@Override
public Broker getBroker() {
if (_broker == null || _broker.isClosed()) {
if (_brokerFactory != null) {
_broker = _brokerFactory.newBroker();
}
}
return _broker;
}
@Override
public void closeBroker() {
if (_broker != null && !_broker.isClosed()) {
_broker.setAutoDetach(AutoDetach.DETACH_CLOSE);
_broker.close();
_broker = null;
}
}
@Override
public OpenJPAStateManager getOwnerStateManager() {
return _ownerSm;
}
@Override
public boolean isDetached() {
return _detached;
}
public boolean isDelayLoad() {
return ProxyCollections.isDelayed(this);
}
// //////////////////////////////////////
// Implementation method wrappers
// //////////////////////////////////////
@Override
public Object clone() {
if (_directAccess) {
return super.clone();
}
if (isDelayLoad()) {
load();
}
Proxy localProxy = (Proxy) super.clone();
localProxy.setOwner(null, 0);
return localProxy;
}
@Override
public void add(int paramInt, Object paramObject) {
if (!_directAccess) {
if (isDelayLoad()) {
load();
}
}
ProxyCollections.beforeAdd(this, paramInt, paramObject);
super.add(paramInt, paramObject);
}
@Override
public boolean add(Object paramObject) {
if (_directAccess) {
return super.add(paramObject);
}
ProxyCollections.beforeAdd(this, paramObject);
boolean bool = super.add(paramObject);
return ProxyCollections.afterAdd(this, paramObject, bool);
}
@Override
public void clear() {
if (!_directAccess) {
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeClear(this);
}
super.clear();
}
@Override
public boolean addAll(Collection paramCollection) {
if (_directAccess) {
return super.addAll(paramCollection);
}
return ProxyCollections.addAll(this, paramCollection);
}
@Override
public boolean addAll(int paramInt, Collection paramCollection) {
if (_directAccess) {
return super.addAll(paramInt, paramCollection);
}
if (isDelayLoad()) {
load();
}
return ProxyCollections.addAll(this, paramInt, paramCollection);
}
@Override
public boolean remove(Object paramObject) {
if (_directAccess) {
return super.remove(paramObject);
}
ProxyCollections.beforeRemove(this, paramObject);
setDirectAccess(true);
boolean bool = super.remove(paramObject);
setDirectAccess(false);
return ProxyCollections.afterRemove(this, paramObject, bool);
}
@Override
public Object remove(int paramInt) {
if (_directAccess) {
return super.remove(paramInt);
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeRemove(this, paramInt);
Object localObject = super.remove(paramInt);
return ProxyCollections.afterRemove(this, paramInt, localObject);
}
@Override
public Object remove() {
if (_directAccess) {
return super.remove();
}
// queue operations require proper ordering. the collection
// must be loaded in order to ensure order.
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeRemove(this);
Object localObject = super.remove();
return ProxyCollections.afterRemove(this, localObject);
}
@Override
public Object set(int paramInt, Object paramObject) {
if (_directAccess) {
return super.set(paramInt, paramObject);
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeSet(this, paramInt, paramObject);
Object localObject = super.set(paramInt, paramObject);
return ProxyCollections.afterSet(this, paramInt, paramObject,
localObject);
}
@Override
public Object poll() {
if (_directAccess) {
return super.poll();
}
// queue operations require proper ordering. the collection
// must be loaded in order to ensure order.
if (isDelayLoad()) {
load();
}
ProxyCollections.beforePoll(this);
Object localObject = super.poll();
return ProxyCollections.afterPoll(this, localObject);
}
@Override
public ListIterator listIterator(int paramInt) {
if (_directAccess) {
return super.listIterator(paramInt);
}
if (isDelayLoad()) {
load();
}
ListIterator localListIterator = super.listIterator(paramInt);
return ProxyCollections.afterListIterator(this, paramInt,
localListIterator);
}
@Override
public void addFirst(Object paramObject) {
if (_directAccess) {
super.addFirst(paramObject);
return;
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeAddFirst(this, paramObject);
super.addFirst(paramObject);
}
@Override
public void addLast(Object paramObject) {
if (_directAccess) {
super.addLast(paramObject);
return;
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeAddLast(this, paramObject);
super.addLast(paramObject);
ProxyCollections.afterAddLast(this, paramObject);
}
@Override
public boolean offer(Object paramObject) {
if (_directAccess) {
return super.offer(paramObject);
}
ProxyCollections.beforeOffer(this, paramObject);
boolean bool = super.offer(paramObject);
return ProxyCollections.afterOffer(this, paramObject, bool);
}
@Override
public Object removeFirst() {
if (_directAccess) {
return super.removeFirst();
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeRemoveFirst(this);
Object localObject = super.removeFirst();
return ProxyCollections.afterRemoveFirst(this, localObject);
}
@Override
public Object removeLast() {
if (_directAccess) {
return super.removeLast();
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeRemoveLast(this);
Object localObject = super.removeLast();
return ProxyCollections.afterRemoveLast(this, localObject);
}
@Override
public Iterator iterator() {
if (_directAccess) {
return super.iterator();
}
if (isDelayLoad()) {
load();
}
Iterator localIterator = super.iterator();
return ProxyCollections.afterIterator(this, localIterator);
}
@Override
public ListIterator listIterator() {
if (_directAccess) {
return super.listIterator();
}
if (isDelayLoad()) {
load();
}
ListIterator localListIterator = super.listIterator();
return ProxyCollections.afterListIterator(this, localListIterator);
}
@Override
public boolean removeAll(Collection paramCollection) {
if (_directAccess) {
return super.removeAll(paramCollection);
}
return ProxyCollections.removeAll(this, paramCollection);
}
@Override
public boolean retainAll(Collection paramCollection) {
if (_directAccess) {
return super.retainAll(paramCollection);
}
if (isDelayLoad()) {
load();
}
return ProxyCollections.retainAll(this, paramCollection);
}
@Override
public boolean removeFirstOccurrence(Object paramObject) {
if (_directAccess) {
return super.removeFirstOccurrence(paramObject);
}
if (isDelayLoad()) {
load();
}
Proxies.dirty(this, true);
return super.removeFirstOccurrence(paramObject);
}
@Override
public boolean removeLastOccurrence(Object paramObject) {
if (_directAccess) {
return super.removeLastOccurrence(paramObject);
}
if (isDelayLoad()) {
load();
}
Proxies.dirty(this, true);
return super.removeLastOccurrence(paramObject);
}
protected Object writeReplace() throws ObjectStreamException {
if (isDelayLoad()) {
load();
}
return Proxies.writeReplace(this, true);
}
@Override
public boolean equals(Object paramObject) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.equals(paramObject);
}
@Override
public int hashCode() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.hashCode();
}
@Override
public List subList(int fromIndex, int toIndex) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.subList(fromIndex, toIndex);
}
@Override
public int lastIndexOf(Object o) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.lastIndexOf(o);
}
@Override
public int indexOf(Object o) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.indexOf(o);
}
@Override
public Object get(int index) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.get(index);
}
@Override
public boolean containsAll(Collection c) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.containsAll(c);
}
@Override
public Object[] toArray() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray();
}
@Override
public Object[] toArray(Object[] array) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray(array);
}
@Override
public boolean contains(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.contains(object);
}
@Override
public boolean isEmpty() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.isEmpty();
}
@Override
public int size() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.size();
}
@Override
public boolean offerFirst(Object paramObject) {
if (_directAccess) {
return super.offerFirst(paramObject);
}
if (isDelayLoad()) {
load();
}
return super.offerFirst(paramObject);
}
@Override
public boolean offerLast(Object paramObject) {
if (_directAccess) {
return super.offerLast(paramObject);
}
if (isDelayLoad()) {
load();
}
return super.offerLast(paramObject);
}
@Override
public Object pollFirst() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.pollFirst();
}
@Override
public Object pollLast() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.pollLast();
}
@Override
public Object getFirst() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.getFirst();
}
@Override
public Object getLast() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.getLast();
}
@Override
public Object peekFirst() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.peekFirst();
}
@Override
public Object peekLast() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.peekLast();
}
@Override
public Object element() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.element();
}
@Override
public Object peek() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.peek();
}
@Override
public void push(Object o) {
if (!_directAccess && isDelayLoad()) {
load();
}
super.push(o);
}
@Override
public Object pop() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.pop();
}
@Override
public Iterator descendingIterator() {
if (_directAccess) {
return super.descendingIterator();
}
if (isDelayLoad()) {
load();
}
Iterator localIterator = super.descendingIterator();
return ProxyCollections.afterIterator(this, localIterator);
}
}

View File

@ -0,0 +1,429 @@
/*
* 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.util;
import java.io.ObjectStreamException;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.SortedSet;
import org.apache.openjpa.kernel.AutoDetach;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.kernel.BrokerFactory;
import org.apache.openjpa.kernel.DetachedStateManager;
import org.apache.openjpa.kernel.OpenJPAStateManager;
/**
* PriorityQueue proxy with delay loading capability. Allows non-indexed
* add and remove operations to occur on an unloaded collection. Operations
* that require a load will trigger a load.
*/
@SuppressWarnings({"rawtypes","unchecked"})
public class DelayedPriorityQueueProxy extends PriorityQueue implements ProxyCollection, DelayedProxy {
private transient OpenJPAStateManager sm;
private transient int field;
private transient CollectionChangeTracker changeTracker;
private transient Class elementType;
private transient OpenJPAStateManager _ownerSm;
private transient boolean _directAccess = false;
private transient BrokerFactory _brokerFactory = null;
private transient Broker _broker = null;
private transient OpenJPAStateManager _delayedSm;
private transient int _delayedField;
private transient boolean _detached = false;
public DelayedPriorityQueueProxy(int paramInt) {
super(paramInt);
}
public DelayedPriorityQueueProxy(int paramInt, Comparator paramComparator) {
super(paramInt, paramComparator);
}
public DelayedPriorityQueueProxy(Collection paramCollection) {
super(paramCollection);
}
public DelayedPriorityQueueProxy(PriorityQueue paramPriorityQueue) {
super(paramPriorityQueue);
}
public DelayedPriorityQueueProxy(SortedSet paramSortedSet) {
super(paramSortedSet);
}
public DelayedPriorityQueueProxy() {
}
@Override
public void setOwner(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
// If clearing the owner of this proxy, store away what is necessary for
// delayed loading
if (sm != null && detaching(paramOpenJPAStateManager, paramInt)) {
_detached = true;
_delayedSm = sm;
_delayedField = field;
} else {
_detached = false;
}
this.sm = paramOpenJPAStateManager;
if (sm != null && sm.getPersistenceCapable() != null) {
_ownerSm = (OpenJPAStateManager) sm.getPersistenceCapable()
.pcGetStateManager();
}
this.field = paramInt;
if (sm != null && sm.getContext() != null) {
_brokerFactory = sm.getContext().getBroker().getBrokerFactory();
}
}
private boolean detaching(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
if ((paramOpenJPAStateManager == null && paramInt == -1)
|| (paramOpenJPAStateManager != null && paramOpenJPAStateManager instanceof DetachedStateManager)) {
return true;
}
return false;
}
public OpenJPAStateManager getOwner() {
return this.sm;
}
public int getOwnerField() {
return this.field;
}
@Override
public Object clone() throws CloneNotSupportedException {
if (_directAccess) {
return super.clone();
}
if (isDelayLoad()) {
load();
}
Proxy localProxy = (Proxy) super.clone();
localProxy.setOwner(null, 0);
return localProxy;
}
public ChangeTracker getChangeTracker() {
return this.changeTracker;
}
public Object copy(Object paramObject) {
return new PriorityQueue((PriorityQueue) paramObject);
}
public Class getElementType() {
return this.elementType;
}
public ProxyCollection newInstance(Class paramClass,
Comparator paramComparator, boolean paramBoolean1,
boolean paramBoolean2) {
DelayedPriorityQueueProxy localproxy = new DelayedPriorityQueueProxy();
localproxy.elementType = paramClass;
if (paramBoolean1)
localproxy.changeTracker = new DelayedCollectionChangeTrackerImpl(
localproxy, true, false, paramBoolean2);
return localproxy;
}
@Override
public boolean add(Object paramObject) {
if (_directAccess) {
return super.add(paramObject);
}
ProxyCollections.beforeAdd(this, paramObject);
boolean bool = false;
try {
setDirectAccess(true);
bool = super.add(paramObject);
} finally {
setDirectAccess(false);
}
return ProxyCollections.afterAdd(this, paramObject, bool);
}
@Override
public void clear() {
if (!_directAccess) {
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeClear(this);
}
super.clear();
}
@Override
public Iterator iterator() {
if (_directAccess) {
return super.iterator();
}
if (isDelayLoad()) {
load();
}
Iterator localIterator = super.iterator();
return ProxyCollections.afterIterator(this, localIterator);
}
@Override
public boolean remove(Object paramObject) {
if (_directAccess) {
return super.remove(paramObject);
}
ProxyCollections.beforeRemove(this, paramObject);
setDirectAccess(true);
boolean bool = super.remove(paramObject);
setDirectAccess(false);
return ProxyCollections.afterRemove(this, paramObject, bool);
}
@Override
public Object poll() {
if (_directAccess) {
return super.poll();
}
// queue operations require proper ordering. the collection
// must be loaded in order to ensure order.
if (isDelayLoad()) {
load();
}
ProxyCollections.beforePoll(this);
Object localObject = super.poll();
return ProxyCollections.afterPoll(this, localObject);
}
@Override
public boolean offer(Object paramObject) {
if (_directAccess) {
return super.offer(paramObject);
}
ProxyCollections.beforeOffer(this, paramObject);
boolean bool = super.offer(paramObject);
return ProxyCollections.afterOffer(this, paramObject, bool);
}
@Override
public boolean addAll(Collection paramCollection) {
if (_directAccess) {
return super.addAll(paramCollection);
}
return ProxyCollections.addAll(this, paramCollection);
}
@Override
public Object remove() {
if (_directAccess) {
return super.remove();
}
// queue operations require proper ordering. the collection
// must be loaded in order to ensure order.
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeRemove(this);
Object localObject = super.remove();
return ProxyCollections.afterRemove(this, localObject);
}
@Override
public boolean removeAll(Collection paramCollection) {
if (_directAccess) {
return super.removeAll(paramCollection);
}
return ProxyCollections.removeAll(this, paramCollection);
}
@Override
public boolean retainAll(Collection paramCollection) {
if (_directAccess) {
return super.retainAll(paramCollection);
}
if (isDelayLoad()) {
load();
}
return ProxyCollections.retainAll(this, paramCollection);
}
protected Object writeReplace() throws ObjectStreamException {
if (isDelayLoad()) {
load();
}
return Proxies.writeReplace(this, true);
}
@Override
public int size() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.size();
}
@Override
public boolean isEmpty() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.isEmpty();
}
@Override
public boolean contains(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.contains(object);
}
@Override
public Object[] toArray() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray();
}
@Override
public Object[] toArray(Object[] array) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray(array);
}
@Override
public boolean containsAll(Collection c) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.containsAll(c);
}
@Override
public Object element() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.element();
}
@Override
public Object peek() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.peek();
}
@Override
public boolean equals(Object paramObject) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.equals(paramObject);
}
@Override
public int hashCode() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.hashCode();
}
// //////////////////////////////////////
// DelayedProxy methods
// //////////////////////////////////////
@Override
public int getDelayedField() {
if (field == -1 || _detached) {
return _delayedField;
}
return field;
}
@Override
public OpenJPAStateManager getDelayedOwner() {
if (sm == null || _detached) {
return _delayedSm;
}
return sm;
}
@Override
public boolean isDirectAccess() {
return _directAccess;
}
@Override
public void setDirectAccess(boolean direct) {
_directAccess = direct;
}
public BrokerFactory getBrokerFactory() {
return _brokerFactory;
}
@Override
public void load() {
ProxyCollections.loadCollection(this);
}
@Override
public Broker getBroker() {
if (_broker == null || _broker.isClosed()) {
if (_brokerFactory != null) {
_broker = _brokerFactory.newBroker();
}
}
return _broker;
}
@Override
public void closeBroker() {
if (_broker != null && !_broker.isClosed()) {
_broker.setAutoDetach(AutoDetach.DETACH_CLOSE);
_broker.close();
_broker = null;
}
}
@Override
public OpenJPAStateManager getOwnerStateManager() {
return _ownerSm;
}
@Override
public boolean isDetached() {
return _detached;
}
public boolean isDelayLoad() {
return ProxyCollections.isDelayed(this);
}
}

View File

@ -21,23 +21,64 @@ package org.apache.openjpa.util;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.kernel.OpenJPAStateManager;
/**
* Implemented by proxy classes which are delay-load capable. Delay-load
* proxies are lazily loaded and provide some operations
* which allow manipulation of proxy without necessarily needing to
* load the proxied object.
*/
public interface DelayedProxy {
/**
* Load the proxy if it was delay-loaded.
*/
void load();
/**
* Returns whether the caller has direct-call access to the proxied
* object. Direct access allows calls to be made on the object
* without triggering a load or proxy state tracking callbacks.
*/
boolean isDirectAccess();
/**
* Sets whether the caller has direct-call access to the proxied
* object. Direct access allows calls to be made on the object
* without triggering a load or proxy state tracking callbacks.
*/
void setDirectAccess(boolean direct);
/**
* Get the broker that is used to service this proxy.
*/
Broker getBroker();
/**
* Close the broker that is used to service this proxy.
*/
void closeBroker();
/**
* Returns the state manager of the owning instance.
*/
OpenJPAStateManager getOwnerStateManager();
/**
* Returns a state manager that can service this proxy even if
* the collection was detached.
*/
OpenJPAStateManager getDelayedOwner();
/**
* Returns the expected field index even if this collection
* was detached.
* @return
*/
int getDelayedField();
/**
* Returns whether the proxy is detached.
* @return
*/
boolean isDetached();
}

View File

@ -0,0 +1,500 @@
/*
* 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.util;
import java.io.ObjectStreamException;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.TreeSet;
import org.apache.openjpa.kernel.AutoDetach;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.kernel.BrokerFactory;
import org.apache.openjpa.kernel.DetachedStateManager;
import org.apache.openjpa.kernel.OpenJPAStateManager;
/**
* TreeSet proxy with delay loading capability. Allows non-indexed
* add and remove operations to occur on an unloaded collection. Operations
* that require a load will trigger a load.
*/
@SuppressWarnings({"rawtypes","unchecked"})
public class DelayedTreeSetProxy extends TreeSet implements ProxyCollection, DelayedProxy {
private transient OpenJPAStateManager sm;
private transient int field;
private transient CollectionChangeTracker changeTracker;
private transient Class elementType;
private transient OpenJPAStateManager _ownerSm;
private transient boolean _directAccess = false;
private transient BrokerFactory _brokerFactory = null;
private transient Broker _broker = null;
private transient OpenJPAStateManager _delayedSm;
private transient int _delayedField;
private transient boolean _detached = false;
public DelayedTreeSetProxy() {
}
public DelayedTreeSetProxy(Comparator paramComparator) {
super(paramComparator);
}
public DelayedTreeSetProxy(Collection paramCollection) {
super(paramCollection);
}
public DelayedTreeSetProxy(SortedSet paramSortedSet) {
super(paramSortedSet);
}
public void setOwner(OpenJPAStateManager paramOpenJPAStateManager, int paramInt)
{
// If clearing the owner of this proxy, store away what is necessary for
// delayed loading
if (sm != null && detaching(paramOpenJPAStateManager, paramInt)) {
_detached = true;
_delayedSm = sm;
_delayedField = field;
} else {
_detached = false;
}
this.sm = paramOpenJPAStateManager;
if (sm != null && sm.getPersistenceCapable() != null) {
_ownerSm = (OpenJPAStateManager) sm.getPersistenceCapable().pcGetStateManager();
}
this.field = paramInt;
if (sm != null && sm.getContext() != null) {
_brokerFactory = sm.getContext().getBroker().getBrokerFactory();
}
}
private boolean detaching(OpenJPAStateManager paramOpenJPAStateManager, int paramInt) {
if ((paramOpenJPAStateManager == null && paramInt == -1) ||
(paramOpenJPAStateManager != null && paramOpenJPAStateManager instanceof DetachedStateManager)) {
return true;
}
return false;
}
public OpenJPAStateManager getOwner() {
return this.sm;
}
public int getOwnerField() {
return this.field;
}
public ChangeTracker getChangeTracker() {
return this.changeTracker;
}
public Object copy(Object paramObject) {
return new TreeSet((SortedSet) paramObject);
}
public Class getElementType() {
return this.elementType;
}
public ProxyCollection newInstance(Class paramClass,
Comparator paramComparator, boolean paramBoolean1,
boolean paramBoolean2) {
DelayedTreeSetProxy localproxy = new DelayedTreeSetProxy(
paramComparator);
localproxy.elementType = paramClass;
if (paramBoolean1)
localproxy.changeTracker = new DelayedCollectionChangeTrackerImpl(
localproxy, false, false, paramBoolean2);
return localproxy;
}
@Override
public Object clone() {
if (_directAccess) {
return super.clone();
}
if (isDelayLoad()) {
load();
}
Proxy localProxy = (Proxy) super.clone();
localProxy.setOwner(null, 0);
return localProxy;
}
protected Object writeReplace() throws ObjectStreamException {
if (isDelayLoad()) {
load();
}
return Proxies.writeReplace(this, true);
}
////////////////////////////////////////
// DelayedProxy methods
////////////////////////////////////////
public int getDelayedField() {
if (field == -1 || _detached) {
return _delayedField;
}
return field;
}
public OpenJPAStateManager getDelayedOwner() {
if (sm == null || _detached) {
return _delayedSm;
}
return sm;
}
public boolean isDirectAccess() {
return _directAccess;
}
public void setDirectAccess(boolean direct) {
_directAccess = direct;
}
public BrokerFactory getBrokerFactory() {
return _brokerFactory;
}
@Override
public void load() {
ProxyCollections.loadCollection(this);
}
@Override
public Broker getBroker() {
if (_broker == null || _broker.isClosed()) {
if (_brokerFactory != null) {
_broker = _brokerFactory.newBroker();
}
}
return _broker;
}
@Override
public void closeBroker() {
if (_broker != null && !_broker.isClosed()) {
_broker.setAutoDetach(AutoDetach.DETACH_CLOSE);
_broker.close();
_broker = null;
}
}
@Override
public OpenJPAStateManager getOwnerStateManager() {
return _ownerSm;
}
@Override
public boolean isDetached() {
return _detached;
}
public boolean isDelayLoad() {
return ProxyCollections.isDelayed(this);
}
////////////////////////////////////////
// TreeSet methods
////////////////////////////////////////
@Override
public void clear() {
if (!_directAccess) {
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeClear(this);
}
super.clear();
}
@Override
public Iterator iterator() {
if (_directAccess) {
return super.iterator();
}
if (isDelayLoad()) {
load();
}
Iterator localIterator = super.iterator();
return ProxyCollections.afterIterator(this, localIterator);
}
@Override
public boolean remove(Object paramObject) {
if (_directAccess) {
return super.remove(paramObject);
}
ProxyCollections.beforeRemove(this, paramObject);
setDirectAccess(true);
boolean bool = super.remove(paramObject);
setDirectAccess(false);
return ProxyCollections.afterRemove(this, paramObject, bool);
}
@Override
public int size() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.size();
}
@Override
public boolean isEmpty() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.isEmpty();
}
@Override
public boolean contains(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.contains(object);
}
@Override
public Object[] toArray() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray();
}
@Override
public Object[] toArray(Object[] array) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray(array);
}
@Override
public Comparator comparator() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.comparator();
}
@Override
public Object first() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.first();
}
@Override
public Object last() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.last();
}
@Override
public boolean add(Object paramObject) {
if (_directAccess) {
return super.add(paramObject);
}
ProxyCollections.beforeAdd(this, paramObject);
boolean bool = super.add(paramObject);
return ProxyCollections.afterAdd(this, paramObject, bool);
}
@Override
public boolean containsAll(Collection c) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.containsAll(c);
}
@Override
public boolean addAll(Collection paramCollection) {
if (_directAccess) {
return super.addAll(paramCollection);
}
return ProxyCollections.addAll(this, paramCollection);
}
@Override
public boolean retainAll(Collection paramCollection) {
if (_directAccess) {
return super.retainAll(paramCollection);
}
if (isDelayLoad()) {
load();
}
return ProxyCollections.retainAll(this, paramCollection);
}
@Override
public boolean removeAll(Collection paramCollection) {
if (_directAccess) {
return super.removeAll(paramCollection);
}
return ProxyCollections.removeAll(this, paramCollection);
}
@Override
public Object lower(Object e) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.lower(e);
}
@Override
public Object floor(Object e) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.floor(e);
}
@Override
public Object ceiling(Object e) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.ceiling(e);
}
@Override
public Object higher(Object e) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.higher(e);
}
@Override
public Object pollFirst() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.pollFirst();
}
@Override
public Object pollLast() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.pollLast();
}
@Override
public NavigableSet descendingSet() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.descendingSet();
}
@Override
public Iterator descendingIterator() {
if (_directAccess) {
return super.descendingIterator();
}
if (isDelayLoad()) {
load();
}
Iterator localIterator = super.descendingIterator();
return ProxyCollections.afterIterator(this, localIterator);
}
@Override
public NavigableSet subSet(Object fromElement, boolean fromInclusive,
Object toElement, boolean toInclusive) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.subSet(fromElement, fromInclusive, toElement, toInclusive);
}
@Override
public NavigableSet headSet(Object toElement, boolean inclusive) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.headSet(toElement, inclusive);
}
@Override
public NavigableSet tailSet(Object fromElement, boolean inclusive) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.tailSet(fromElement, inclusive);
}
@Override
public SortedSet subSet(Object fromElement, Object toElement) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.subSet(fromElement, toElement);
}
@Override
public SortedSet headSet(Object toElement) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.headSet(toElement);
}
@Override
public SortedSet tailSet(Object fromElement) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.tailSet(fromElement);
}
@Override
public boolean equals(Object paramObject) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.equals(paramObject);
}
@Override
public int hashCode() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.hashCode();
}
}

View File

@ -0,0 +1,653 @@
/*
* 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.util;
import java.io.ObjectStreamException;
import java.util.Collection;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
import org.apache.openjpa.kernel.AutoDetach;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.kernel.BrokerFactory;
import org.apache.openjpa.kernel.DetachedStateManager;
import org.apache.openjpa.kernel.OpenJPAStateManager;
/**
* Vector proxy with delay loading capability. Allows non-indexed
* add and remove operations to occur on an unloaded collection. Operations
* that require a load will trigger a load.
*/
@SuppressWarnings({"rawtypes","unchecked"})
public class DelayedVectorProxy extends Vector implements ProxyCollection, DelayedProxy {
private transient OpenJPAStateManager sm;
private transient int field;
private transient CollectionChangeTracker changeTracker;
private transient Class elementType;
private transient OpenJPAStateManager _ownerSm;
private transient boolean _directAccess = false;
private transient BrokerFactory _brokerFactory = null;
private transient Broker _broker = null;
private transient OpenJPAStateManager _delayedSm;
private transient int _delayedField;
private transient boolean _detached = false;
public DelayedVectorProxy(int paramInt) {
super(paramInt);
}
public DelayedVectorProxy() {
}
public DelayedVectorProxy(Collection paramCollection) {
super(paramCollection);
}
public DelayedVectorProxy(int paramInt1, int paramInt2) {
super(paramInt1, paramInt2);
}
@Override
public void setOwner(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
// If clearing the owner of this proxy, store away what is necessary for
// delayed loading
if (sm != null && detaching(paramOpenJPAStateManager, paramInt)) {
_detached = true;
_delayedSm = sm;
_delayedField = field;
} else {
_detached = false;
}
this.sm = paramOpenJPAStateManager;
if (sm != null && sm.getPersistenceCapable() != null) {
_ownerSm = (OpenJPAStateManager) sm.getPersistenceCapable()
.pcGetStateManager();
}
this.field = paramInt;
if (sm != null && sm.getContext() != null) {
_brokerFactory = sm.getContext().getBroker().getBrokerFactory();
}
}
private boolean detaching(OpenJPAStateManager paramOpenJPAStateManager,
int paramInt) {
if ((paramOpenJPAStateManager == null && paramInt == -1)
|| (paramOpenJPAStateManager != null && paramOpenJPAStateManager instanceof DetachedStateManager)) {
return true;
}
return false;
}
@Override
public int getDelayedField() {
if (field == -1 || _detached) {
return _delayedField;
}
return field;
}
@Override
public OpenJPAStateManager getDelayedOwner() {
if (sm == null || _detached) {
return _delayedSm;
}
return sm;
}
@Override
public OpenJPAStateManager getOwner() {
return this.sm;
}
@Override
public int getOwnerField() {
return this.field;
}
@Override
public boolean isDirectAccess() {
return _directAccess;
}
@Override
public void setDirectAccess(boolean direct) {
_directAccess = direct;
}
public BrokerFactory getBrokerFactory() {
return _brokerFactory;
}
@Override
public void load() {
ProxyCollections.loadCollection(this);
}
@Override
public Broker getBroker() {
if (_broker == null || _broker.isClosed()) {
if (_brokerFactory != null) {
_broker = _brokerFactory.newBroker();
}
}
return _broker;
}
@Override
public void closeBroker() {
if (_broker != null && !_broker.isClosed()) {
_broker.setAutoDetach(AutoDetach.DETACH_CLOSE);
_broker.close();
_broker = null;
}
}
@Override
public OpenJPAStateManager getOwnerStateManager() {
return _ownerSm;
}
@Override
public boolean isDetached() {
return _detached;
}
public boolean isDelayLoad() {
return ProxyCollections.isDelayed(this);
}
@Override
public synchronized Object clone() {
if (isDirectAccess()) {
return super.clone();
}
if (isDelayLoad()) {
load();
}
Proxy localProxy = (Proxy) super.clone();
localProxy.setOwner(null, 0);
return localProxy;
}
@Override
public ChangeTracker getChangeTracker() {
return this.changeTracker;
}
@Override
public Object copy(Object paramObject) {
if (isDelayLoad()) {
load();
}
return new Vector((Collection) paramObject);
}
@Override
public Class getElementType() {
return this.elementType;
}
@Override
public ProxyCollection newInstance(Class paramClass,
Comparator paramComparator, boolean paramBoolean1,
boolean paramBoolean2) {
DelayedVectorProxy localproxy = new DelayedVectorProxy();
localproxy.elementType = paramClass;
if (paramBoolean1) {
localproxy.changeTracker = new DelayedCollectionChangeTrackerImpl(
localproxy, true, true, paramBoolean2);
}
return localproxy;
}
@Override
public synchronized boolean add(Object paramObject) {
if (_directAccess) {
return super.add(paramObject);
}
ProxyCollections.beforeAdd(this, paramObject);
boolean bool = super.add(paramObject);
return ProxyCollections.afterAdd(this, paramObject, bool);
}
@Override
public synchronized void add(int paramInt, Object paramObject) {
if (!_directAccess) {
if (isDelayLoad()) {
load();
}
}
ProxyCollections.beforeAdd(this, paramInt, paramObject);
super.add(paramInt, paramObject);
}
@Override
public void clear() {
if (!_directAccess) {
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeClear(this);
}
super.clear();
}
@Override
public synchronized boolean addAll(int paramInt, Collection paramCollection) {
if (_directAccess) {
return super.addAll(paramInt, paramCollection);
}
if (isDelayLoad()) {
load();
}
return ProxyCollections.addAll(this, paramInt, paramCollection);
}
@Override
public synchronized boolean addAll(Collection paramCollection) {
if (_directAccess) {
return super.addAll(paramCollection);
}
return ProxyCollections.addAll(this, paramCollection);
}
@Override
public synchronized void addElement(Object paramObject) {
if (_directAccess) {
super.addElement(paramObject);
return;
}
ProxyCollections.beforeAddElement(this, paramObject);
super.addElement(paramObject);
ProxyCollections.afterAddElement(this, paramObject);
}
@Override
public synchronized Object remove(int paramInt) {
if (_directAccess) {
return super.remove(paramInt);
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeRemove(this, paramInt);
Object localObject = super.remove(paramInt);
return ProxyCollections.afterRemove(this, paramInt, localObject);
}
@Override
public synchronized boolean remove(Object paramObject) {
if (_directAccess) {
return super.remove(paramObject);
}
ProxyCollections.beforeRemove(this, paramObject);
setDirectAccess(true);
boolean bool = super.remove(paramObject);
setDirectAccess(false);
return ProxyCollections.afterRemove(this, paramObject, bool);
}
@Override
public synchronized Object set(int paramInt, Object paramObject) {
if (_directAccess) {
return super.set(paramInt, paramObject);
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeSet(this, paramInt, paramObject);
Object localObject = super.set(paramInt, paramObject);
return ProxyCollections.afterSet(this, paramInt, paramObject,
localObject);
}
@Override
public synchronized boolean removeAll(Collection paramCollection) {
if (_directAccess) {
return super.removeAll(paramCollection);
}
return ProxyCollections.removeAll(this, paramCollection);
}
@Override
public synchronized boolean retainAll(Collection paramCollection) {
if (_directAccess) {
return super.retainAll(paramCollection);
}
if (isDelayLoad()) {
load();
}
return ProxyCollections.retainAll(this, paramCollection);
}
@Override
public synchronized void insertElementAt(Object paramObject, int paramInt) {
if (_directAccess) {
super.insertElementAt(paramObject, paramInt);
return;
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeInsertElementAt(this, paramObject, paramInt);
super.insertElementAt(paramObject, paramInt);
}
@Override
public synchronized void removeAllElements() {
if (_directAccess) {
super.removeAllElements();
return;
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeRemoveAllElements(this);
super.removeAllElements();
}
@Override
public synchronized boolean removeElement(Object paramObject) {
if (_directAccess) {
return super.removeElement(paramObject);
}
ProxyCollections.beforeRemoveElement(this, paramObject);
setDirectAccess(true);
boolean bool = super.removeElement(paramObject);
setDirectAccess(false);
return ProxyCollections.afterRemoveElement(this, paramObject, bool);
}
@Override
public synchronized void removeElementAt(int paramInt) {
if (_directAccess) {
super.removeElementAt(paramInt);
return;
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeRemoveElementAt(this, paramInt);
super.removeElementAt(paramInt);
}
@Override
public synchronized void setElementAt(Object paramObject, int paramInt) {
if (_directAccess) {
super.setElementAt(paramObject, paramInt);
return;
}
if (isDelayLoad()) {
load();
}
ProxyCollections.beforeSetElementAt(this, paramObject, paramInt);
super.setElementAt(paramObject, paramInt);
}
@Override
public Iterator iterator() {
if (_directAccess) {
return super.iterator();
}
if (isDelayLoad()) {
load();
}
Iterator localIterator = super.iterator();
return ProxyCollections.afterIterator(this, localIterator);
}
@Override
public ListIterator listIterator(int paramInt) {
if (_directAccess) {
return super.listIterator(paramInt);
}
if (isDelayLoad()) {
load();
}
ListIterator localListIterator = super.listIterator(paramInt);
return ProxyCollections.afterListIterator(this, paramInt,
localListIterator);
}
@Override
public ListIterator listIterator() {
if (_directAccess) {
return super.listIterator();
}
if (isDelayLoad()) {
load();
}
ListIterator localListIterator = super.listIterator();
return ProxyCollections.afterListIterator(this, localListIterator);
}
@Override
public synchronized void setSize(int paramInt) {
if (_directAccess) {
super.setSize(paramInt);
return;
}
if (isDelayLoad()) {
load();
}
Proxies.dirty(this, true);
super.setSize(paramInt);
}
protected synchronized Object writeReplace() throws ObjectStreamException {
if (isDelayLoad()) {
load();
}
return Proxies.writeReplace(this, true);
}
@Override
public synchronized boolean contains(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.contains(object);
}
@Override
public synchronized boolean containsAll(Collection collection) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.containsAll(collection);
}
@Override
public synchronized boolean isEmpty() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.isEmpty();
}
@Override
public synchronized int size() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.size();
}
@Override
public synchronized Object[] toArray() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray();
}
@Override
public synchronized Object[] toArray(Object[] array) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toArray(array);
}
@Override
public synchronized boolean equals(Object paramObject) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.equals(paramObject);
}
@Override
public synchronized int hashCode() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.hashCode();
}
@Override
public synchronized int lastIndexOf(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.lastIndexOf(object);
}
@Override
public synchronized List subList(int start, int end) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.subList(start, end);
}
@Override
public synchronized Object get(int location) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.get(location);
}
@Override
public synchronized int indexOf(Object object) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.indexOf(object);
}
@Override
public synchronized int indexOf(Object object, int index) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.indexOf(object, index);
}
@Override
public synchronized void copyInto(Object[] anArray) {
if (!_directAccess && isDelayLoad()) {
load();
}
super.copyInto(anArray);
}
public synchronized void trimToSize() {
if (!_directAccess && isDelayLoad()) {
load();
}
super.trimToSize();
}
public synchronized void ensureCapacity(int minCapacity) {
if (!_directAccess && isDelayLoad()) {
load();
}
super.ensureCapacity(minCapacity);
}
public synchronized int capacity() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.capacity();
}
public Enumeration elements() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.elements();
}
public synchronized int lastIndexOf(Object o, int index) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.lastIndexOf(o, index);
}
public synchronized Object elementAt(int index) {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.elementAt(index);
}
public synchronized Object firstElement() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.firstElement();
}
public synchronized Object lastElement() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.lastElement();
}
public synchronized String toString() {
if (!_directAccess && isDelayLoad()) {
load();
}
return super.toString();
}
protected synchronized void removeRange(int fromIndex, int toIndex) {
if (!_directAccess && isDelayLoad()) {
load();
}
super.removeRange(fromIndex, toIndex);
}
}

View File

@ -497,14 +497,43 @@ public class ProxyManagerImpl
*/
protected Class loadBuildTimeProxy(Class type, ClassLoader loader) {
try {
if (_delayedCollectionLoading && type.equals(java.util.ArrayList.class)) {
return org.apache.openjpa.util.DelayedArrayListProxy.class;
Class<?> proxyClass = null;
if (_delayedCollectionLoading) {
proxyClass = loadDelayedProxy(type);
if (proxyClass != null) {
return proxyClass;
}
}
return Class.forName(getProxyClassName(type, false), true, loader);
} catch (Throwable t) {
return null;
}
}
protected Class<?> loadDelayedProxy(Class<?> type) {
if (type.equals(java.util.ArrayList.class)) {
return org.apache.openjpa.util.DelayedArrayListProxy.class;
}
if (type.equals(java.util.HashSet.class)) {
return org.apache.openjpa.util.DelayedHashSetProxy.class;
}
if (type.equals(java.util.LinkedList.class)) {
return org.apache.openjpa.util.DelayedLinkedListProxy.class;
}
if (type.equals(java.util.Vector.class)) {
return org.apache.openjpa.util.DelayedVectorProxy.class;
}
if (type.equals(java.util.LinkedHashSet.class)) {
return org.apache.openjpa.util.DelayedLinkedHashSetProxy.class;
}
if (type.equals(java.util.SortedSet.class) || type.equals(java.util.TreeSet.class)) {
return org.apache.openjpa.util.DelayedTreeSetProxy.class;
}
if (type.equals(java.util.PriorityQueue.class)) {
return org.apache.openjpa.util.DelayedPriorityQueueProxy.class;
}
return null;
}
/**
* Instantiate the given proxy class.

View File

@ -0,0 +1,56 @@
/*
* 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.proxy.delayed;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class Award implements Serializable, Comparable<Award>{
private static final long serialVersionUID = -1110613520812966568L;
private String awdName;
private String awdType;
public void setAwdName(String awdName) {
this.awdName = awdName;
}
public String getAwdName() {
return awdName;
}
public void setAwdType(String awdType) {
this.awdType = awdType;
}
public String getAwdType() {
return awdType;
}
@Override
public int compareTo(Award o) {
String nameType = awdName+awdType;
String nameType2 = o.getAwdName()+o.getAwdType();
return nameType.compareTo(nameType2);
}
}

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.proxy.delayed;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Embeddable;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Embeddable
public class Certification implements Serializable, Comparable<Certification> {
private static final long serialVersionUID = 4989402309885734073L;
private String name;
private String level;
@Temporal(TemporalType.DATE)
private Date certDate;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setLevel(String level) {
this.level = level;
}
public String getLevel() {
return level;
}
public void setCertDate(Date certDate) {
this.certDate = certDate;
}
public Date getCertDate() {
return certDate;
}
@Override
public int compareTo(Certification o) {
String nameLevelDate = name+level+certDate;
String nameLevelDate2 = o.getName()+o.getLevel()+o.getCertDate();
return nameLevelDate.compareTo(nameLevelDate2);
}
}

View File

@ -0,0 +1,810 @@
/*
* 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.proxy.delayed;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import javax.persistence.EntityManager;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.kernel.DetachedStateManager;
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
import org.apache.openjpa.util.DelayedProxy;
import org.apache.openjpa.util.Proxy;
import org.apache.openjpa.util.ProxyCollection;
/**
* Verifies generic delay-load capabilities for delay-load collection proxies.
*/
public abstract class DelayedProxyCollectionsTestCase extends SQLListenerTestCase {
protected static Set<String> _ignoreMethods;
protected static Set<String> _delayMethods;
protected static Set<Class<?>> _ignoreInterfaces;
public void setUp(Object...props) {
List<Object> parms = new ArrayList<Object>();
parms.addAll(Arrays.asList(
CLEAR_TABLES,
"openjpa.ProxyManager", "delayCollectionLoading=true",
Award.class,
Location.class,
Product.class,
Certification.class));
parms.addAll(Arrays.asList(props));
super.setUp(parms.toArray());
}
public abstract IAccount createAccount(String name, IUserIdentity ui);
public abstract IDepartment createDepartment();
public abstract IDepartment findDepartment(EntityManager em, int id);
public abstract IEmployee createEmployee();
public abstract Collection<IEmployee> createEmployees();
public abstract IMember createMember(String name);
public abstract IUserIdentity findUserIdentity(EntityManager em, int id);
public abstract IUserIdentity createUserIdentity();
public abstract Collection<Product> createProducts();
public abstract Collection<Certification> createCertifications();
public abstract IEmployee getEmployee(Collection<IEmployee> emps, int idx);
public abstract Collection<Award> createAwards();
public abstract Product getProduct(Collection<Product> products, int idx);
public abstract Collection<Location> createLocations();
public abstract Collection<IAccount> createAccounts();
static {
// non-indexed delay-capable methods
_delayMethods = new HashSet<String>();
// generic collection
_delayMethods.add(stringMethodName("add", new Class<?>[] {Object.class}));
_delayMethods.add(stringMethodName("remove", new Class<?>[] {Object.class}));
_delayMethods.add(stringMethodName("removeAll", new Class<?>[] {Collection.class}));
_delayMethods.add(stringMethodName("addAll", new Class<?>[] {Collection.class}));
// queue
_delayMethods.add(stringMethodName("offer", new Class<?>[] {Object.class}));
// vector
_delayMethods.add(stringMethodName("addElement", new Class<?>[] {Object.class}));
_delayMethods.add(stringMethodName("removeElement", new Class<?>[] {Object.class}));
// non-trigger methods
_ignoreMethods = new HashSet<String>();
_ignoreMethods.add(stringMethodName("trimToSize", null));
_ignoreMethods.add(stringMethodName("ensureCapacity", new Class<?>[] {int.class}));
_ignoreMethods.add(stringMethodName("comparator", null));
// non-trigger base Object methods
_ignoreMethods.add(stringMethodName("wait", new Class<?>[] {long.class}));
_ignoreMethods.add(stringMethodName("wait", null));
_ignoreMethods.add(stringMethodName("wait", new Class<?>[] {long.class, int.class}));
_ignoreMethods.add(stringMethodName("getClass", null));
_ignoreMethods.add(stringMethodName("notify", null));
_ignoreMethods.add(stringMethodName("notifyAll", null));
_ignoreInterfaces = new HashSet<Class<?>>();
_ignoreInterfaces.add(DelayedProxy.class);
_ignoreInterfaces.add(Proxy.class);
_ignoreInterfaces.add(ProxyCollection.class);
}
public static String stringMethodName(Method m) {
return stringMethodName(m.getName(), m.getParameterTypes());
}
public static String stringMethodName(String m, Class<?>[] types) {
StringBuilder sb = new StringBuilder(m);
if (types != null) {
for (Class<?> type : types) {
sb.append(":");
sb.append(type.getName());
}
}
return sb.toString();
}
public Set<String> methodsToIgnore() {
return _ignoreMethods;
}
public Award createAward() {
Award a = new Award();
a.setAwdName("Employee of the Month " + new Random().nextInt(999999));
a.setAwdType("Certificate");
return a;
}
public Certification createCertification() {
Certification c = new Certification();
c.setName("Certification XYZ " + new Random().nextInt(999999));
c.setCertDate(new Date());
return c;
}
public Product createProduct() {
Product p = new Product();
p.setName("Product : " + new Random().nextInt(999999));
return p;
}
public Location createLocation() {
Location l = new Location();
l.setAddress(new Random().nextInt(9999) + " Wandering Way");
l.setCity("Somewhere");
l.setZip(Integer.toString(new Random().nextInt(99999)));
return l;
}
/*
* Verify an element can be non-index removed from a delayed proxy collection
* without triggering a load of the collection.
*/
public void testSingleRemove() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
IEmployee e2 = createEmployee();
e2.setDept(d);
e2.setEmpName("Joe");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
emps.add(e2);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
em.clear();
resetSQL();
d = findDepartment(em, d.getId());
// assert the select did not contain the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertNotNull(d);
emps = d.getEmployees();
// assert there was no select
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedProxy);
DelayedProxy dep = (DelayedProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, emps.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// remove the employee from the collection
resetSQL();
em.getTransaction().begin();
emps.remove(e);
em.getTransaction().commit();
// assert the delete from the join table
assertAnySQLAnyOrder("DELETE FROM DC_DEP_EMP .*");
// assert no select from employee or dept table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*", "SELECT .* DC_DEPARTMENT .*");
// iterate the collection and assert a select from the employee table
// and that the expected entity is returned
resetSQL();
assertEquals(1, emps.size());
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
IEmployee e3 = getEmployee(emps, 0);
assertEquals(e2, e3);
em.close();
}
/*
* Verify an element can be non-index added to a delayed proxy collection
* without triggering a load on the collection.
*/
public void testSingleAdd() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
em.clear();
resetSQL();
d = findDepartment(em, d.getId());
// assert the select did not contain the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertNotNull(d);
emps = d.getEmployees();
// assert there was no select
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedProxy);
DelayedProxy dep = (DelayedProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, emps.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// add an employee to the collection
resetSQL();
em.getTransaction().begin();
IEmployee e2 = createEmployee();
e2.setDept(d);
e2.setEmpName("Joe");
emps.add(e2);
em.getTransaction().commit();
// assert the insert into the employee and join table
assertAnySQLAnyOrder("INSERT INTO DC_DEP_EMP .*");
assertAnySQLAnyOrder("INSERT INTO DC_EMPLOYEE .*");
// assert no select from employee or dept table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*", "SELECT .* DC_DEPARTMENT .*");
// call contains and assert a select from the employee table
// occurred that the expected entities are returned.
resetSQL();
assertTrue(emps.contains(e));
assertTrue(emps.contains(e2));
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
resetSQL();
assertEquals(2, emps.size());
// verify a second SQL was not issued to get the size
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
em.close();
}
/*
* Verify a mix of non-indexed add and remove operations can occur without
* triggering a load on a delayed collection.
*/
public void testMixedAddRemove() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
em.clear();
resetSQL();
d = findDepartment(em, d.getId());
// assert the select did not contain the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertNotNull(d);
emps = d.getEmployees();
// assert there was no select
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedProxy);
DelayedProxy dep = (DelayedProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, emps.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// add an employee to the collection and remove the same employee and commit
resetSQL();
em.getTransaction().begin();
IEmployee e2 = createEmployee();
e2.setDept(d);
e2.setEmpName("Joe");
emps.add(e2);
emps.remove(e2);
em.getTransaction().commit();
// assert the insert into the entity and join table
assertNoneSQLAnyOrder("INSERT INTO DC_DEP_EMP .*");
assertNoneSQLAnyOrder("INSERT INTO DC_EMPLOYEE .*");
// assert no select from employee or dept table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*", "SELECT .* DC_DEPARTMENT .*");
// add two employees to the collection and remove one and commit
resetSQL();
em.getTransaction().begin();
IEmployee e3 = createEmployee();
e3.setDept(d);
e3.setEmpName("Rhonda");
emps.add(e3);
IEmployee e4 = createEmployee();
e4.setDept(d);
e4.setEmpName("Maria");
emps.add(e4);
emps.remove(e3);
em.getTransaction().commit();
// assert the insert into the employee and join table
assertAnySQLAnyOrder("INSERT INTO DC_DEP_EMP .*");
assertAnySQLAnyOrder("INSERT INTO DC_EMPLOYEE .*");
// assert no select from employee or dept table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*", "SELECT .* DC_DEPARTMENT .*");
// call contains and assert a select from the employee table
// occurred that the expected entities are returned.
resetSQL();
assertTrue(emps.contains(e));
assertFalse(emps.contains(e2));
assertFalse(emps.contains(e3));
assertTrue(emps.contains(e4));
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
resetSQL();
assertEquals(2, emps.size());
// verify a second SQL was not issued to get the size
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
em.close();
}
/*
* Verify that an eagerly loaded collection with delayed load enabled
* functions as expected.
*/
public void testEagerCollection() {
EntityManager em = emf.createEntityManager();
// Create a new department and
IDepartment d = createDepartment();
Collection<Product> products = createProducts();
Product p = createProduct();
products.add(p);
Product p2 = createProduct();
products.add(p2);
d.setProducts(products);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
resetSQL();
em.clear();
d = findDepartment(em, d.getId());
assertAnySQLAnyOrder("SELECT .* DC_DEP_PRD .*");
resetSQL();
products = d.getProducts();
assertTrue(products instanceof DelayedProxy);
ProxyCollection pxycoll = (ProxyCollection)products;
assertFalse(pxycoll.getOwner().isDelayed(pxycoll.getOwnerField()));
Product p3 = getProduct(products, 0);
assertTrue(products.contains(p3));
assertEquals(2, products.size());
assertNoneSQLAnyOrder("SELECT .* DC_DEPARTMENT .*", "SELECT .* DC_DEP_PRD .*");
em.close();
}
/*
* Verify that a DB ordered collection is not delay load capable.
*/
public void testOrderedCollection() {
EntityManager em = emf.createEntityManager();
// Create a new department and persist
IDepartment d = createDepartment();
Location l = createLocation();
Collection<Location> locs = createLocations();
locs.add(l);
d.setLocations(locs);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
em.clear();
d = findDepartment(em, d.getId());
assertNoneSQLAnyOrder("SELECT .* DC_DEP_LOC .*");
// verify that the collection is not delay loaded and does not trigger a load
resetSQL();
Collection<Location> locations = d.getLocations();
assertAnySQLAnyOrder("SELECT .* DC_DEP_LOC .*");
resetSQL();
assertTrue(locations instanceof DelayedProxy);
ProxyCollection pxycoll = (ProxyCollection)locations;
assertFalse(pxycoll.getOwner().isDelayed(pxycoll.getOwnerField()));
assertEquals(1, locations.size());
assertNoneSQLAnyOrder("SELECT .* DC_DEPARTMENT .*", "SELECT .* DC_DEP_LOC .*");
em.close();
}
/*
* Verify that a collection will load upon serialization
*/
public void testSerialization() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
em.clear();
resetSQL();
d = findDepartment(em, d.getId());
// assert the select did not contain the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertNotNull(d);
emps = d.getEmployees();
// assert there was no select
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedProxy);
DelayedProxy dep = (DelayedProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, emps.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// add an employee to the collection
resetSQL();
em.getTransaction().begin();
IEmployee e2 = createEmployee();
e2.setDept(d);
e2.setEmpName("Joe");
emps.add(e2);
em.getTransaction().commit();
// assert the insert into the employee and join table
assertAnySQLAnyOrder("INSERT INTO DC_DEP_EMP .*");
assertAnySQLAnyOrder("INSERT INTO DC_EMPLOYEE .*");
// assert no select from employee or dept table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*", "SELECT .* DC_DEPARTMENT .*");
resetSQL();
try {
// Serialize the department entity and verify the employee collection was loaded
IDepartment d2 = roundtrip(d);
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*", "SELECT .* DC_DEP_EMP .*");
emps = d2.getEmployees();
assertTrue(emps.contains(e));
assertTrue(emps.contains(e2));
assertEquals(2, emps.size());
} catch (Exception ex) {
fail(ex.getMessage());
}
em.close();
}
/*
* Verify that a lazy collection of embeddables works as expected
* (delays load) with delayed loading enabled
*/
public void testLazyEmbeddableCollection() {
EntityManager em = emf.createEntityManager();
IDepartment d = createDepartment();
Collection<Certification> certs = createCertifications();
certs.add(createCertification());
certs.add(createCertification());
d.setCertifications(certs);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
resetSQL();
em.clear();
d = findDepartment(em, d.getId());
assertNoneSQLAnyOrder("SELECT .* DC_DEP_CERT .*");
resetSQL();
certs = d.getCertifications();
assertNoneSQLAnyOrder("SELECT .* DC_DEP_CERT .*");
assertTrue(certs instanceof DelayedProxy);
assertEquals(2,certs.size());
assertAnySQLAnyOrder("SELECT .* DC_DEP_CERT .*");
em.close();
}
/*
* Verify that an eager collection of embeddables works as expected
* (no delay load) with delayed loading enabled
*/
public void testEagerEmbeddableCollection() {
EntityManager em = emf.createEntityManager();
IDepartment d = createDepartment();
Collection<Award> awards = createAwards();
awards.add(createAward());
awards.add(createAward());
awards.add(createAward());
d.setAwards(awards);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
resetSQL();
em.clear();
d = findDepartment(em, d.getId());
assertAnySQLAnyOrder("SELECT .* DC_DEP_AWD .*");
resetSQL();
awards = d.getAwards();
ProxyCollection pxycoll = (ProxyCollection)awards;
assertFalse(pxycoll.getOwner().isDelayed(pxycoll.getOwnerField()));
assertNoneSQLAnyOrder("SELECT .* DC_DEP_AWD .*");
assertTrue(awards instanceof DelayedProxy);
assertEquals(3,awards.size());
assertNoneSQLAnyOrder("SELECT .* DC_DEP_AWD .*");
em.close();
}
/*
* Verify that a collection can be loaded post detachment
*/
public void testPostDetach() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
resetSQL();
em.clear();
d = findDepartment(em, d.getId());
emps = d.getEmployees();
em.close();
// assert there was no select on the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedProxy);
DelayedProxy dep = (DelayedProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, emps.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// call contains and assert a select from the employee table
// occurred that the expected entities are returned.
resetSQL();
assertTrue(emps.contains(e));
e = getEmployee(emps, 0);
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
resetSQL();
assertEquals(1, emps.size());
// Verify the delay load entity is detached
assertTrue(e instanceof PersistenceCapable);
PersistenceCapable pc = (PersistenceCapable)e;
assertTrue(pc.pcGetStateManager() instanceof DetachedStateManager);
// verify a second SQL was not issued to get the size
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
// add a employee to the collection and merge
IEmployee e2 = createEmployee();
e2.setDept(d);
emps.add(e2);
em = emf.createEntityManager();
em.getTransaction().begin();
em.merge(d);
em.getTransaction().commit();
emps = d.getEmployees();
// assert the insert into the employee and join table
assertAnySQLAnyOrder("INSERT INTO DC_DEP_EMP .*");
assertAnySQLAnyOrder("INSERT INTO DC_EMPLOYEE .*");
assertEquals(2, emps.size());
em.close();
// remove an employee from the collection and merge
emps.remove(e);
em = emf.createEntityManager();
em.getTransaction().begin();
em.merge(d);
em.getTransaction().commit();
emps = d.getEmployees();
// assert the delete from the join table
assertAnySQLAnyOrder("DELETE FROM DC_DEP_EMP .*");
assertEquals(1, emps.size());
em.close();
}
/*
* Verify that a lazy collection within an embeddable can be
* delayed. The to-many in the embeddable uses
*/
public void testEmbeddableRelationship() {
EntityManager em = emf.createEntityManager();
IUserIdentity ui = createUserIdentity();
IMember m = createMember("Member 1");
ui.setMember(m);
Collection<IAccount> accounts = createAccounts();
IAccount checking = createAccount("Checking", ui);
accounts.add(checking);
IAccount savings = createAccount("Savings", ui);
accounts.add(savings);
em.getTransaction().begin();
em.persist(ui);
em.persist(checking);
em.persist(savings);
em.getTransaction().commit();
em.clear();
ui = findUserIdentity(em, ui.getId());
m = ui.getMember();
resetSQL();
accounts = m.getAccounts();
ProxyCollection pxycoll = (ProxyCollection)accounts;
assertTrue(pxycoll.getOwner().isDelayed(pxycoll.getOwnerField()));
assertNoneSQLAnyOrder("SELECT .* DC_ACCOUNT .*");
assertTrue(accounts instanceof DelayedProxy);
// Trigger a load via iterator
int count = 0;
for (IAccount a : accounts) {
count++;
}
assertEquals(2,count);
assertAnySQLAnyOrder("SELECT .* DC_ACCOUNT .*");
em.close();
}
/**
* Verifies proxy methods which require loading the collection will trigger a
* load.
*/
public void testProxyMethods() {
// Load up a collection
EntityManager em = emf.createEntityManager();
// Create a new department and employees
IDepartment d = createDepartment();
Collection<IEmployee> emps = createEmployees();
for (int i = 0; i < 50; i++) {
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("Employee: " + i);
emps.add(e);
}
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
em.clear();
resetSQL();
// build a list of public proxy methods
// exclude those methods that are certain not to cause a load
// add(Object) remove(Object), addAll(Collection), removeAll(Collection), poll?, copy()
Class<?> collType = emps.getClass();
Method[] methods = collType.getMethods();
for (Method m : methods) {
if (!excludeMethod(m)) {
buildAndInvoke(m, em, d.getId(), emps);
}
}
em.close();
}
private void buildAndInvoke(Method m, EntityManager em, int id, Collection<IEmployee> emps) {
em.clear();
resetSQL();
IDepartment d = findDepartment(em, id);
Collection<?> emps2 = d.getEmployees();
assertTrue(emps2 instanceof DelayedProxy);
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
try {
m.invoke(emps2, buildArgs(m, emps));
// not checking result or exception, just whether load was triggered
} catch (Throwable t) {
// gulp
}
if (_delayMethods.contains(stringMethodName(m))) {
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
} else {
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
}
}
/**
* Build up a set of generic args just to get the basic calls through.
*/
private Object[] buildArgs(Method m, Collection<?> emps) {
Class<?>[] parmTypes = m.getParameterTypes();
if (parmTypes == null) {
return new Object[]{};
}
int intNum = 0;
int objNum = 0;
Object[] parms = new Object[parmTypes.length];
for (int i = 0; i < parmTypes.length; i++) {
Class<?> parmType = parmTypes[i];
if (parmTypes[i].equals(int.class)) {
parms[i] = intNum;
intNum++;
continue;
}
if (parmTypes[i].equals(boolean.class)) {
parms[i] = true;
continue;
}
if (parmTypes[i].equals(Object.class)) {
parms[i] = emps.toArray()[objNum];
objNum++;
continue;
}
if (parmTypes[i].isAssignableFrom(Collection.class)) {
parms[i] = emps;
continue;
}
}
return parms;
}
/*
* Determines whether a proxy method should be invoked
*/
private boolean excludeMethod(Method m) {
if(_ignoreInterfaces.contains(m.getDeclaringClass())) {
return true;
}
if (_ignoreMethods.contains(stringMethodName(m))) {
return true;
}
return false;
}
}

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.proxy.delayed;
public interface IAccount {
public void setId(int id);
public int getId();
public void setName(String name);
public String getName();
public void setUserIdent(IUserIdentity userIdent);
public IUserIdentity getUserIdent();
}

View File

@ -0,0 +1,52 @@
/*
* 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.proxy.delayed;
import java.util.Collection;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Table;
public interface IDepartment {
public void setEmployees(Collection<IEmployee> employees);
public Collection<IEmployee> getEmployees();
public void setId(int id);
public int getId();
public void setLocations(Collection<Location> locations);
public Collection<Location> getLocations();
public void setProducts(Collection<Product> products);
public Collection<Product> getProducts();
public void setCertifications(Collection<Certification> certifications);
public Collection<Certification> getCertifications();
public void setAwards(Collection<Award> awards);
public Collection<Award> getAwards();
}

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.proxy.delayed;
public interface IEmployee {
public void setEmpName(String empName);
public String getEmpName();
public void setId(int id);
public int getId();
public void setDept(IDepartment dept);
public IDepartment getDept();
}

View File

@ -0,0 +1,32 @@
/*
* 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.proxy.delayed;
import java.util.Collection;
public interface IMember {
public void setName(String name);
public String getName();
public void setAccounts(Collection<IAccount> accounts);
public Collection<IAccount> getAccounts();
}

View File

@ -0,0 +1,30 @@
/*
* 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.proxy.delayed;
public interface IUserIdentity {
public void setMember(IMember member);
public IMember getMember();
public void setId(int id);
public int getId();
}

View File

@ -0,0 +1,83 @@
/*
* 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.proxy.delayed;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="DC_LOCATION")
public class Location implements Serializable, Comparable<Location> {
private static final long serialVersionUID = -8396529344135184546L;
@Id
@GeneratedValue
@Column(name="LOC_ID")
private int id;
private String address;
private String city;
private String zip;
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getZip() {
return zip;
}
@Override
public int compareTo(Location l) {
return new Integer(getId()).compareTo(l.getId());
}
}

View File

@ -0,0 +1,61 @@
/*
* 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.proxy.delayed;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Product implements Serializable, Comparable<Product> {
private static final long serialVersionUID = 8353220697329535861L;
@Id
@GeneratedValue
private int id;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public int compareTo(Product p) {
String nameId = name + id;
String nameId2 = p.getName() + p.getId();
return nameId.compareTo(nameId2);
}
}

View File

@ -0,0 +1,79 @@
/*
* 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.proxy.delayed.alist;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_ACCOUNT")
public class Account implements IAccount {
public Account() {
}
public Account(String name, IUserIdentity uid) {
setName(name);
setUserIdent(uid);
}
@Id
@GeneratedValue
@Column(name="ACCT_ID")
private int id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="UID_ID")
private UserIdentity userIdent;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setUserIdent(IUserIdentity userIdent) {
this.userIdent = (UserIdentity)userIdent;
}
public IUserIdentity getUserIdent() {
return userIdent;
}
}

View File

@ -0,0 +1,135 @@
/*
* 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.proxy.delayed.alist;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
@Entity
@Table(name="DC_DEPARTMENT")
public class Department implements IDepartment, Serializable {
private static final long serialVersionUID = -6923551949033215888L;
@Id
@GeneratedValue
private int id;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY, targetEntity=Employee.class)
@JoinTable(name="DC_DEP_EMP")
private List<IEmployee> employees;
@OrderColumn
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
@JoinTable(name="DC_DEP_LOC")
private List<Location> locations;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(name="DC_DEP_PRD")
private List<Product> products;
@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name="DC_DEP_CERT")
private List<Certification> certifications;
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name="DC_DEP_AWD")
private List<Award> awards;
@Override
public void setEmployees(Collection<IEmployee> employees) {
this.employees = (List<IEmployee>)employees;
}
@Override
public Collection<IEmployee> getEmployees() {
return employees;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public int getId() {
return id;
}
@Override
public void setLocations(Collection<Location> locations) {
this.locations =(List<Location>)locations;
}
@Override
public Collection<Location> getLocations() {
return locations;
}
@Override
public void setProducts(Collection<Product> products) {
this.products = (List<Product>)products;
}
@Override
public Collection<Product> getProducts() {
return products;
}
@Override
public void setCertifications(Collection<Certification> certifications) {
this.certifications = (List<Certification>)certifications;
}
@Override
public Collection<Certification> getCertifications() {
return certifications;
}
@Override
public void setAwards(Collection<Award> awards) {
this.awards = (List<Award>)awards;
}
@Override
public Collection<Award> getAwards() {
return awards;
}
}

View File

@ -0,0 +1,84 @@
/*
* 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.proxy.delayed.alist;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
@Entity
@Table(name="DC_EMPLOYEE")
public class Employee implements IEmployee, Serializable {
private static final long serialVersionUID = 1878272252981151246L;
@Id
@GeneratedValue
private int id;
private String empName;
@ManyToOne(targetEntity=Department.class)
@JoinColumn(name="DEPT_ID")
private IDepartment dept;
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpName() {
return empName;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setDept(IDepartment dept) {
this.dept = dept;
}
public IDepartment getDept() {
return dept;
}
public boolean equals(Object obj) {
if (obj instanceof Employee) {
Employee e = (Employee)obj;
return e.getId() == getId() && e.getEmpName().equals(getEmpName());
}
return false;
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.proxy.delayed.alist;
import java.util.Collection;
import java.util.List;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
@Embeddable
public class Member implements IMember {
private String name;
@OneToMany(fetch=FetchType.LAZY, mappedBy="userIdent", targetEntity=Account.class)
private List<IAccount> accounts;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAccounts(Collection<IAccount> accounts) {
this.accounts = (List<IAccount>)accounts;
}
public Collection<IAccount> getAccounts() {
return accounts;
}
}

View File

@ -0,0 +1,136 @@
/*
* 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.proxy.delayed.alist;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.DelayedProxyCollectionsTestCase;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
public class TestDelayedArrayListProxy extends DelayedProxyCollectionsTestCase {
public static Object[] _pcList = {
Employee.class,
Department.class,
UserIdentity.class,
Member.class,
Account.class
};
public void setUp() {
super.setUp(_pcList);
}
public void setUp(Object... props){
List<Object> parms = new ArrayList<Object>();
// Add package-specific types
parms.addAll(Arrays.asList(_pcList));
// Add properties from super
parms.addAll(Arrays.asList(props));
super.setUp(parms.toArray());
}
public IUserIdentity findUserIdentity(EntityManager em, int id) {
return em.find(UserIdentity.class, id);
}
public IDepartment findDepartment(EntityManager em, int id) {
return em.find(Department.class, id);
}
public IUserIdentity createUserIdentity() {
UserIdentity ui = new UserIdentity();
return ui;
}
public IAccount createAccount(String name, IUserIdentity ui) {
IAccount acct = new Account(name, ui);
return acct;
}
public IDepartment createDepartment() {
Department d = new Department();
return d;
}
public IMember createMember(String name) {
Member m = new Member();
m.setName(name);
return m;
}
@Override
public IEmployee createEmployee() {
Employee e = new Employee();
return e;
}
@Override
public Collection<IEmployee> createEmployees() {
return new ArrayList<IEmployee>();
}
@Override
public Collection<Product> createProducts() {
return new ArrayList<Product>();
}
@Override
public Collection<Award> createAwards() {
return new ArrayList<Award>();
}
@Override
public Collection<Location> createLocations() {
return new ArrayList<Location>();
}
@Override
public Collection<Certification> createCertifications() {
return new ArrayList<Certification>();
}
@Override
public Collection<IAccount> createAccounts() {
return new ArrayList<IAccount>();
}
@Override
public IEmployee getEmployee(Collection<IEmployee> emps, int idx) {
return ((List<IEmployee>)emps).get(idx);
}
@Override
public Product getProduct(Collection<Product> products, int idx) {
return ((List<Product>)products).get(idx);
}
}

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.proxy.delayed.alist;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.persistence.proxy.delayed.DelayedProxyCollectionsTestCase;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.util.DelayedArrayListProxy;
public class TestDelayedArrayListProxyDetachLite extends TestDelayedArrayListProxy {
@Override
public void setUp() {
super.setUp(
"openjpa.DetachState", "loaded(LiteAutoDetach=true,detachProxyFields=false)");
}
/*
* Verify that a collection can be loaded post detachment
*/
@Override
public void testPostDetach() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
resetSQL();
em.clear();
d = findDepartment(em, d.getId());
emps = d.getEmployees();
em.close();
// assert there was no select on the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedArrayListProxy);
DelayedArrayListProxy dep = (DelayedArrayListProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, dep.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// call contains and assert a select from the employee table
// occurred that the expected entities are returned.
resetSQL();
assertTrue(emps.contains(e));
e = getEmployee(emps,0);
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
resetSQL();
assertEquals(1, emps.size());
// Verify the delay load entity is detached
assertTrue(e instanceof PersistenceCapable);
PersistenceCapable pc = (PersistenceCapable)e;
// LiteAutoDetach
assertTrue(pc.pcGetStateManager() == null);
// verify a second SQL was not issued to get the size
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.proxy.delayed.alist;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_UIDENT")
public class UserIdentity implements IUserIdentity {
@Id
@GeneratedValue
@Column(name="UID_ID")
private int id;
@Embedded
private Member member;
public void setMember(IMember member) {
this.member = (Member)member;
}
public IMember getMember() {
return member;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}

View File

@ -0,0 +1,79 @@
/*
* 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.proxy.delayed.hset;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_ACCOUNT")
public class Account implements IAccount {
public Account() {
}
public Account(String name, IUserIdentity uid) {
setName(name);
setUserIdent(uid);
}
@Id
@GeneratedValue
@Column(name="ACCT_ID")
private int id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="UID_ID")
private UserIdentity userIdent;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setUserIdent(IUserIdentity userIdent) {
this.userIdent = (UserIdentity)userIdent;
}
public IUserIdentity getUserIdent() {
return userIdent;
}
}

View File

@ -0,0 +1,134 @@
/*
* 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.proxy.delayed.hset;
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
@Entity
@Table(name="DC_DEPARTMENT")
public class Department implements IDepartment, Serializable {
private static final long serialVersionUID = -6923551949033215888L;
@Id
@GeneratedValue
private int id;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY, targetEntity=Employee.class)
@JoinTable(name="DC_DEP_EMP")
private Set<IEmployee> employees;
@OrderColumn
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
@JoinTable(name="DC_DEP_LOC")
private Set<Location> locations;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(name="DC_DEP_PRD")
private Set<Product> products;
@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name="DC_DEP_CERT")
private Set<Certification> certifications;
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name="DC_DEP_AWD")
private Set<Award> awards;
@Override
public void setEmployees(Collection<IEmployee> employees) {
this.employees = (Set<IEmployee>)employees;
}
@Override
public Collection<IEmployee> getEmployees() {
return employees;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public int getId() {
return id;
}
@Override
public void setLocations(Collection<Location> locations) {
this.locations =(Set<Location>)locations;
}
@Override
public Collection<Location> getLocations() {
return locations;
}
@Override
public void setProducts(Collection<Product> products) {
this.products = (Set<Product>)products;
}
@Override
public Collection<Product> getProducts() {
return products;
}
@Override
public void setCertifications(Collection<Certification> certifications) {
this.certifications = (Set<Certification>)certifications;
}
@Override
public Collection<Certification> getCertifications() {
return certifications;
}
@Override
public void setAwards(Collection<Award> awards) {
this.awards = (Set<Award>)awards;
}
@Override
public Collection<Award> getAwards() {
return awards;
}
}

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.proxy.delayed.hset;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
@Entity
@Table(name="DC_EMPLOYEE")
public class Employee implements IEmployee, Serializable {
private static final long serialVersionUID = 1878272252981151246L;
@Id
@GeneratedValue
private int id;
private String empName;
@ManyToOne(targetEntity=Department.class)
@JoinColumn(name="DEPT_ID")
private IDepartment dept;
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpName() {
return empName;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setDept(IDepartment dept) {
this.dept = dept;
}
public IDepartment getDept() {
return dept;
}
@Override
public int hashCode() {
return getId();
}
public boolean equals(Object obj) {
if (obj instanceof Employee) {
Employee e = (Employee)obj;
return e.getId() == getId() && e.getEmpName().equals(getEmpName());
}
return false;
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.proxy.delayed.hset;
import java.util.Collection;
import java.util.Set;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
@Embeddable
public class Member implements IMember {
private String name;
@OneToMany(fetch=FetchType.LAZY, mappedBy="userIdent", targetEntity=Account.class)
private Set<IAccount> accounts;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAccounts(Collection<IAccount> accounts) {
this.accounts = (Set<IAccount>)accounts;
}
public Collection<IAccount> getAccounts() {
return accounts;
}
}

View File

@ -0,0 +1,143 @@
/*
* 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.proxy.delayed.hset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.DelayedProxyCollectionsTestCase;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
public class TestDelayedHashSetProxy extends DelayedProxyCollectionsTestCase {
public static Object[] _pcList = {
Employee.class,
Department.class,
UserIdentity.class,
Member.class,
Account.class
};
public void setUp() {
super.setUp(_pcList);
}
public void setUp(Object... props){
List<Object> parms = new ArrayList<Object>();
// Add package-specific types
parms.addAll(Arrays.asList(_pcList));
// Add properties from super
parms.addAll(Arrays.asList(props));
super.setUp(parms.toArray());
}
public IUserIdentity findUserIdentity(EntityManager em, int id) {
return em.find(UserIdentity.class, id);
}
public IDepartment findDepartment(EntityManager em, int id) {
return em.find(Department.class, id);
}
public IUserIdentity createUserIdentity() {
UserIdentity ui = new UserIdentity();
return ui;
}
public IAccount createAccount(String name, IUserIdentity ui) {
IAccount acct = new Account(name, ui);
return acct;
}
public IDepartment createDepartment() {
Department d = new Department();
return d;
}
public IMember createMember(String name) {
Member m = new Member();
m.setName(name);
return m;
}
@Override
public IEmployee createEmployee() {
Employee e = new Employee();
return e;
}
@Override
public Collection<IEmployee> createEmployees() {
return new HashSet<IEmployee>();
}
@Override
public Collection<Product> createProducts() {
return new HashSet<Product>();
}
@Override
public Collection<Award> createAwards() {
return new HashSet<Award>();
}
@Override
public Collection<Location> createLocations() {
return new HashSet<Location>();
}
@Override
public Collection<Certification> createCertifications() {
return new HashSet<Certification>();
}
@Override
public Collection<IAccount> createAccounts() {
return new HashSet<IAccount>();
}
@Override
public IEmployee getEmployee(Collection<IEmployee> emps, int idx) {
if (emps == null || emps.iterator() == null) {
return null;
}
return emps.iterator().next();
}
@Override
public Product getProduct(Collection<Product> products, int idx) {
if (products == null || products.iterator() == null) {
return null;
}
return products.iterator().next();
}
}

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.proxy.delayed.hset;
import java.util.Collection;
import javax.persistence.EntityManager;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.util.DelayedHashSetProxy;
public class TestDelayedHashSetProxyDetachLite extends TestDelayedHashSetProxy {
@Override
public void setUp() {
super.setUp(
"openjpa.DetachState", "loaded(LiteAutoDetach=true,detachProxyFields=false)");
}
/*
* Verify that a collection can be loaded post detachment
*/
@Override
public void testPostDetach() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
resetSQL();
em.clear();
d = findDepartment(em, d.getId());
emps = d.getEmployees();
em.close();
// assert there was no select on the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedHashSetProxy);
DelayedHashSetProxy dep = (DelayedHashSetProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, dep.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// call contains and assert a select from the employee table
// occurred that the expected entities are returned.
resetSQL();
assertTrue(emps.contains(e));
e = getEmployee(emps,0);
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
resetSQL();
assertEquals(1, emps.size());
// Verify the delay load entity is detached
assertTrue(e instanceof PersistenceCapable);
PersistenceCapable pc = (PersistenceCapable)e;
// LiteAutoDetach
assertTrue(pc.pcGetStateManager() == null);
// verify a second SQL was not issued to get the size
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.proxy.delayed.hset;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_UIDENT")
public class UserIdentity implements IUserIdentity {
@Id
@GeneratedValue
@Column(name="UID_ID")
private int id;
@Embedded
private Member member;
public void setMember(IMember member) {
this.member = (Member)member;
}
public IMember getMember() {
return member;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}

View File

@ -0,0 +1,79 @@
/*
* 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.proxy.delayed.lhset;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_ACCOUNT")
public class Account implements IAccount {
public Account() {
}
public Account(String name, IUserIdentity uid) {
setName(name);
setUserIdent(uid);
}
@Id
@GeneratedValue
@Column(name="ACCT_ID")
private int id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="UID_ID")
private UserIdentity userIdent;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setUserIdent(IUserIdentity userIdent) {
this.userIdent = (UserIdentity)userIdent;
}
public IUserIdentity getUserIdent() {
return userIdent;
}
}

View File

@ -0,0 +1,134 @@
/*
* 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.proxy.delayed.lhset;
import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashSet;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
@Entity
@Table(name="DC_DEPARTMENT")
public class Department implements IDepartment, Serializable {
private static final long serialVersionUID = -6923551949033215888L;
@Id
@GeneratedValue
private int id;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY, targetEntity=Employee.class)
@JoinTable(name="DC_DEP_EMP")
private LinkedHashSet<IEmployee> employees;
@OrderColumn
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
@JoinTable(name="DC_DEP_LOC")
private LinkedHashSet<Location> locations;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(name="DC_DEP_PRD")
private LinkedHashSet<Product> products;
@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name="DC_DEP_CERT")
private LinkedHashSet<Certification> certifications;
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name="DC_DEP_AWD")
private LinkedHashSet<Award> awards;
@Override
public void setEmployees(Collection<IEmployee> employees) {
this.employees = (LinkedHashSet<IEmployee>)employees;
}
@Override
public Collection<IEmployee> getEmployees() {
return employees;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public int getId() {
return id;
}
@Override
public void setLocations(Collection<Location> locations) {
this.locations =(LinkedHashSet<Location>)locations;
}
@Override
public Collection<Location> getLocations() {
return locations;
}
@Override
public void setProducts(Collection<Product> products) {
this.products = (LinkedHashSet<Product>)products;
}
@Override
public Collection<Product> getProducts() {
return products;
}
@Override
public void setCertifications(Collection<Certification> certifications) {
this.certifications = (LinkedHashSet<Certification>)certifications;
}
@Override
public Collection<Certification> getCertifications() {
return certifications;
}
@Override
public void setAwards(Collection<Award> awards) {
this.awards = (LinkedHashSet<Award>)awards;
}
@Override
public Collection<Award> getAwards() {
return awards;
}
}

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.proxy.delayed.lhset;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
@Entity
@Table(name="DC_EMPLOYEE")
public class Employee implements IEmployee, Serializable {
private static final long serialVersionUID = 1878272252981151246L;
@Id
@GeneratedValue
private int id;
private String empName;
@ManyToOne(targetEntity=Department.class)
@JoinColumn(name="DEPT_ID")
private IDepartment dept;
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpName() {
return empName;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setDept(IDepartment dept) {
this.dept = dept;
}
public IDepartment getDept() {
return dept;
}
@Override
public int hashCode() {
return getId();
}
public boolean equals(Object obj) {
if (obj instanceof Employee) {
Employee e = (Employee)obj;
return e.getId() == getId() && e.getEmpName().equals(getEmpName());
}
return false;
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.proxy.delayed.lhset;
import java.util.Collection;
import java.util.LinkedHashSet;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
@Embeddable
public class Member implements IMember {
private String name;
@OneToMany(fetch=FetchType.LAZY, mappedBy="userIdent", targetEntity=Account.class)
private LinkedHashSet<IAccount> accounts;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAccounts(Collection<IAccount> accounts) {
this.accounts = (LinkedHashSet<IAccount>)accounts;
}
public Collection<IAccount> getAccounts() {
return accounts;
}
}

View File

@ -0,0 +1,145 @@
/*
* 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.proxy.delayed.lhset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.DelayedProxyCollectionsTestCase;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
public class TestDelayedLinkedHashSetProxy extends DelayedProxyCollectionsTestCase {
public static Object[] _pcList = {
Employee.class,
Department.class,
UserIdentity.class,
Member.class,
Account.class
};
public void setUp() {
super.setUp(_pcList);
}
public void setUp(Object... props){
List<Object> parms = new ArrayList<Object>();
// Add package-specific types
parms.addAll(Arrays.asList(_pcList));
// Add properties from super
parms.addAll(Arrays.asList(props));
super.setUp(parms.toArray());
}
public IUserIdentity findUserIdentity(EntityManager em, int id) {
return em.find(UserIdentity.class, id);
}
public IDepartment findDepartment(EntityManager em, int id) {
return em.find(Department.class, id);
}
public IUserIdentity createUserIdentity() {
UserIdentity ui = new UserIdentity();
return ui;
}
public IAccount createAccount(String name, IUserIdentity ui) {
IAccount acct = new Account(name, ui);
return acct;
}
public IDepartment createDepartment() {
Department d = new Department();
return d;
}
public IMember createMember(String name) {
Member m = new Member();
m.setName(name);
return m;
}
@Override
public IEmployee createEmployee() {
Employee e = new Employee();
return e;
}
@Override
public Collection<IEmployee> createEmployees() {
return new LinkedHashSet<IEmployee>();
}
@Override
public Collection<Product> createProducts() {
return new LinkedHashSet<Product>();
}
@Override
public Collection<Award> createAwards() {
return new LinkedHashSet<Award>();
}
@Override
public Collection<Location> createLocations() {
return new LinkedHashSet<Location>();
}
@Override
public Collection<Certification> createCertifications() {
return new LinkedHashSet<Certification>();
}
@Override
public Collection<IAccount> createAccounts() {
return new LinkedHashSet<IAccount>();
}
@Override
public IEmployee getEmployee(Collection<IEmployee> emps, int idx) {
if (emps == null || emps.iterator() == null) {
return null;
}
return emps.iterator().next();
}
@Override
public Product getProduct(Collection<Product> products, int idx) {
if (products == null || products.iterator() == null) {
return null;
}
return products.iterator().next();
}
}

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.proxy.delayed.lhset;
import java.util.Collection;
import javax.persistence.EntityManager;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.util.DelayedLinkedHashSetProxy;
public class TestDelayedLinkedHashSetProxyDetachLite extends TestDelayedLinkedHashSetProxy {
@Override
public void setUp() {
super.setUp(
"openjpa.DetachState", "loaded(LiteAutoDetach=true,detachProxyFields=false)");
}
/*
* Verify that a collection can be loaded post detachment
*/
@Override
public void testPostDetach() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
resetSQL();
em.clear();
d = findDepartment(em, d.getId());
emps = d.getEmployees();
em.close();
// assert there was no select on the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedLinkedHashSetProxy);
DelayedLinkedHashSetProxy dep = (DelayedLinkedHashSetProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, dep.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// call contains and assert a select from the employee table
// occurred that the expected entities are returned.
resetSQL();
assertTrue(emps.contains(e));
e = getEmployee(emps,0);
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
resetSQL();
assertEquals(1, emps.size());
// Verify the delay load entity is detached
assertTrue(e instanceof PersistenceCapable);
PersistenceCapable pc = (PersistenceCapable)e;
// LiteAutoDetach
assertTrue(pc.pcGetStateManager() == null);
// verify a second SQL was not issued to get the size
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.proxy.delayed.lhset;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_UIDENT")
public class UserIdentity implements IUserIdentity {
@Id
@GeneratedValue
@Column(name="UID_ID")
private int id;
@Embedded
private Member member;
public void setMember(IMember member) {
this.member = (Member)member;
}
public IMember getMember() {
return member;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}

View File

@ -0,0 +1,79 @@
/*
* 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.proxy.delayed.llist;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_ACCOUNT")
public class Account implements IAccount {
public Account() {
}
public Account(String name, IUserIdentity uid) {
setName(name);
setUserIdent(uid);
}
@Id
@GeneratedValue
@Column(name="ACCT_ID")
private int id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="UID_ID")
private UserIdentity userIdent;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setUserIdent(IUserIdentity userIdent) {
this.userIdent = (UserIdentity)userIdent;
}
public IUserIdentity getUserIdent() {
return userIdent;
}
}

View File

@ -0,0 +1,135 @@
/*
* 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.proxy.delayed.llist;
import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Queue;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
@Entity
@Table(name="DC_DEPARTMENT")
public class Department implements IDepartment, Serializable {
private static final long serialVersionUID = -6923551949033215888L;
@Id
@GeneratedValue
private int id;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY, targetEntity=Employee.class)
@JoinTable(name="DC_DEP_EMP")
private Queue<IEmployee> employees;
@OrderColumn
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
@JoinTable(name="DC_DEP_LOC")
private LinkedList<Location> locations;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(name="DC_DEP_PRD")
private Queue<Product> products;
@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name="DC_DEP_CERT")
private Queue<Certification> certifications;
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name="DC_DEP_AWD")
private LinkedList<Award> awards;
@Override
public void setEmployees(Collection<IEmployee> employees) {
this.employees = (Queue<IEmployee>)employees;
}
@Override
public Collection<IEmployee> getEmployees() {
return employees;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public int getId() {
return id;
}
@Override
public void setLocations(Collection<Location> locations) {
this.locations =(LinkedList<Location>)locations;
}
@Override
public Collection<Location> getLocations() {
return locations;
}
@Override
public void setProducts(Collection<Product> products) {
this.products = (Queue<Product>)products;
}
@Override
public Collection<Product> getProducts() {
return products;
}
@Override
public void setCertifications(Collection<Certification> certifications) {
this.certifications = (Queue<Certification>)certifications;
}
@Override
public Collection<Certification> getCertifications() {
return certifications;
}
@Override
public void setAwards(Collection<Award> awards) {
this.awards = (LinkedList<Award>)awards;
}
@Override
public Collection<Award> getAwards() {
return awards;
}
}

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.proxy.delayed.llist;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
@Entity
@Table(name="DC_EMPLOYEE")
public class Employee implements IEmployee, Serializable {
private static final long serialVersionUID = 1878272252981151246L;
@Id
@GeneratedValue
private int id;
private String empName;
@ManyToOne(targetEntity=Department.class)
@JoinColumn(name="DEPT_ID")
private IDepartment dept;
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpName() {
return empName;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setDept(IDepartment dept) {
this.dept = dept;
}
public IDepartment getDept() {
return dept;
}
@Override
public int hashCode() {
return getId();
}
public boolean equals(Object obj) {
if (obj instanceof Employee) {
Employee e = (Employee)obj;
return e.getId() == getId() && e.getEmpName().equals(getEmpName());
}
return false;
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.proxy.delayed.llist;
import java.util.Collection;
import java.util.Queue;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
@Embeddable
public class Member implements IMember {
private String name;
@OneToMany(fetch=FetchType.LAZY, mappedBy="userIdent", targetEntity=Account.class)
private Queue<IAccount> accounts;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAccounts(Collection<IAccount> accounts) {
this.accounts = (Queue<IAccount>)accounts;
}
public Collection<IAccount> getAccounts() {
return accounts;
}
}

View File

@ -0,0 +1,138 @@
/*
* 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.proxy.delayed.llist;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.DelayedProxyCollectionsTestCase;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
public class TestDelayedLinkedListProxy extends DelayedProxyCollectionsTestCase {
public static Object[] _pcList = {
Employee.class,
Department.class,
UserIdentity.class,
Member.class,
Account.class
};
public void setUp() {
super.setUp(_pcList);
}
public void setUp(Object... props){
List<Object> parms = new ArrayList<Object>();
// Add package-specific types
parms.addAll(Arrays.asList(_pcList));
// Add properties from super
parms.addAll(Arrays.asList(props));
super.setUp(parms.toArray());
}
public IUserIdentity findUserIdentity(EntityManager em, int id) {
return em.find(UserIdentity.class, id);
}
public IDepartment findDepartment(EntityManager em, int id) {
return em.find(Department.class, id);
}
public IUserIdentity createUserIdentity() {
UserIdentity ui = new UserIdentity();
return ui;
}
public IAccount createAccount(String name, IUserIdentity ui) {
IAccount acct = new Account(name, ui);
return acct;
}
public IDepartment createDepartment() {
Department d = new Department();
return d;
}
public IMember createMember(String name) {
Member m = new Member();
m.setName(name);
return m;
}
@Override
public IEmployee createEmployee() {
Employee e = new Employee();
return e;
}
@Override
public Collection<IEmployee> createEmployees() {
return new LinkedList<IEmployee>();
}
@Override
public Collection<Product> createProducts() {
return new LinkedList<Product>();
}
@Override
public Collection<Award> createAwards() {
return new LinkedList<Award>();
}
@Override
public Collection<Location> createLocations() {
return new LinkedList<Location>();
}
@Override
public Collection<Certification> createCertifications() {
return new LinkedList<Certification>();
}
@Override
public Collection<IAccount> createAccounts() {
return new LinkedList<IAccount>();
}
@Override
public IEmployee getEmployee(Collection<IEmployee> emps, int idx) {
return ((LinkedList<IEmployee>)emps).get(idx);
}
@Override
public Product getProduct(Collection<Product> products, int idx) {
return ((LinkedList<Product>)products).get(idx);
}
}

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.proxy.delayed.llist;
import java.util.Collection;
import javax.persistence.EntityManager;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.util.DelayedLinkedListProxy;
public class TestDelayedLinkedListProxyDetachLite extends TestDelayedLinkedListProxy {
@Override
public void setUp() {
super.setUp(
"openjpa.DetachState", "loaded(LiteAutoDetach=true,detachProxyFields=false)");
}
/*
* Verify that a collection can be loaded post detachment
*/
@Override
public void testPostDetach() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
resetSQL();
em.clear();
d = findDepartment(em, d.getId());
emps = d.getEmployees();
em.close();
// assert there was no select on the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedLinkedListProxy);
DelayedLinkedListProxy dep = (DelayedLinkedListProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, dep.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// call contains and assert a select from the employee table
// occurred that the expected entities are returned.
resetSQL();
assertTrue(emps.contains(e));
e = getEmployee(emps,0);
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
resetSQL();
assertEquals(1, emps.size());
// Verify the delay load entity is detached
assertTrue(e instanceof PersistenceCapable);
PersistenceCapable pc = (PersistenceCapable)e;
// LiteAutoDetach
assertTrue(pc.pcGetStateManager() == null);
// verify a second SQL was not issued to get the size
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.proxy.delayed.llist;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_UIDENT")
public class UserIdentity implements IUserIdentity {
@Id
@GeneratedValue
@Column(name="UID_ID")
private int id;
@Embedded
private Member member;
public void setMember(IMember member) {
this.member = (Member)member;
}
public IMember getMember() {
return member;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}

View File

@ -0,0 +1,85 @@
/*
* 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.proxy.delayed.pqueue;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_ACCOUNT")
public class Account implements IAccount, Comparable<Account> {
public Account() {
}
public Account(String name, IUserIdentity uid) {
setName(name);
setUserIdent(uid);
}
@Id
@GeneratedValue
@Column(name="ACCT_ID")
private int id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="UID_ID")
private UserIdentity userIdent;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setUserIdent(IUserIdentity userIdent) {
this.userIdent = (UserIdentity)userIdent;
}
public IUserIdentity getUserIdent() {
return userIdent;
}
@Override
public int compareTo(Account o) {
return new Integer(getId()).compareTo(o.getId());
}
}

View File

@ -0,0 +1,134 @@
/*
* 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.proxy.delayed.pqueue;
import java.io.Serializable;
import java.util.Collection;
import java.util.PriorityQueue;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
@Entity
@Table(name="DC_DEPARTMENT")
public class Department implements IDepartment, Serializable {
private static final long serialVersionUID = -6923551949033215888L;
@Id
@GeneratedValue
private int id;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY, targetEntity=Employee.class)
@JoinTable(name="DC_DEP_EMP")
private PriorityQueue<IEmployee> employees;
@OrderColumn
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
@JoinTable(name="DC_DEP_LOC")
private PriorityQueue<Location> locations;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(name="DC_DEP_PRD")
private PriorityQueue<Product> products;
@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name="DC_DEP_CERT")
private PriorityQueue<Certification> certifications;
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name="DC_DEP_AWD")
private PriorityQueue<Award> awards;
@Override
public void setEmployees(Collection<IEmployee> employees) {
this.employees = (PriorityQueue<IEmployee>)employees;
}
@Override
public Collection<IEmployee> getEmployees() {
return employees;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public int getId() {
return id;
}
@Override
public void setLocations(Collection<Location> locations) {
this.locations =(PriorityQueue<Location>)locations;
}
@Override
public Collection<Location> getLocations() {
return locations;
}
@Override
public void setProducts(Collection<Product> products) {
this.products = (PriorityQueue<Product>)products;
}
@Override
public Collection<Product> getProducts() {
return products;
}
@Override
public void setCertifications(Collection<Certification> certifications) {
this.certifications = (PriorityQueue<Certification>)certifications;
}
@Override
public Collection<Certification> getCertifications() {
return certifications;
}
@Override
public void setAwards(Collection<Award> awards) {
this.awards = (PriorityQueue<Award>)awards;
}
@Override
public Collection<Award> getAwards() {
return awards;
}
}

View File

@ -0,0 +1,96 @@
/*
* 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.proxy.delayed.pqueue;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
@Entity
@Table(name="DC_EMPLOYEE")
public class Employee implements IEmployee, Serializable, Comparable<Employee> {
private static final long serialVersionUID = 1878272252981151246L;
@Id
@GeneratedValue
private int id;
private String empName;
@ManyToOne(targetEntity=Department.class)
@JoinColumn(name="DEPT_ID")
private IDepartment dept;
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpName() {
return empName;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setDept(IDepartment dept) {
this.dept = dept;
}
public IDepartment getDept() {
return dept;
}
@Override
public int hashCode() {
return getId();
}
public boolean equals(Object obj) {
if (obj instanceof Employee) {
Employee e = (Employee)obj;
return e.getId() == getId() && e.getEmpName().equals(getEmpName());
}
return false;
}
@Override
public int compareTo(Employee e) {
String nameId = getEmpName() + getId();
String nameId2 = e.getEmpName() + e.getId();
return nameId.compareTo(nameId2);
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.proxy.delayed.pqueue;
import java.util.Collection;
import java.util.Set;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
@Embeddable
public class Member implements IMember {
private String name;
@OneToMany(fetch=FetchType.LAZY, mappedBy="userIdent", targetEntity=Account.class)
private Set<IAccount> accounts;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAccounts(Collection<IAccount> accounts) {
this.accounts = (Set<IAccount>)accounts;
}
public Collection<IAccount> getAccounts() {
return accounts;
}
}

View File

@ -0,0 +1,143 @@
/*
* 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.proxy.delayed.pqueue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.PriorityQueue;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.DelayedProxyCollectionsTestCase;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
public class TestDelayedPriorityQueueProxy extends DelayedProxyCollectionsTestCase {
public static Object[] _pcList = {
Employee.class,
Department.class,
UserIdentity.class,
Member.class,
Account.class
};
public void setUp() {
super.setUp(_pcList);
}
public void setUp(Object... props){
List<Object> parms = new ArrayList<Object>();
// Add package-specific types
parms.addAll(Arrays.asList(_pcList));
// Add properties from super
parms.addAll(Arrays.asList(props));
super.setUp(parms.toArray());
}
public IUserIdentity findUserIdentity(EntityManager em, int id) {
return em.find(UserIdentity.class, id);
}
public IDepartment findDepartment(EntityManager em, int id) {
return em.find(Department.class, id);
}
public IUserIdentity createUserIdentity() {
UserIdentity ui = new UserIdentity();
return ui;
}
public IAccount createAccount(String name, IUserIdentity ui) {
IAccount acct = new Account(name, ui);
return acct;
}
public IDepartment createDepartment() {
Department d = new Department();
return d;
}
public IMember createMember(String name) {
Member m = new Member();
m.setName(name);
return m;
}
@Override
public IEmployee createEmployee() {
Employee e = new Employee();
return e;
}
@Override
public Collection<IEmployee> createEmployees() {
return new PriorityQueue<IEmployee>();
}
@Override
public Collection<Product> createProducts() {
return new PriorityQueue<Product>();
}
@Override
public Collection<Award> createAwards() {
return new PriorityQueue<Award>();
}
@Override
public Collection<Location> createLocations() {
return new PriorityQueue<Location>();
}
@Override
public Collection<Certification> createCertifications() {
return new PriorityQueue<Certification>();
}
@Override
public Collection<IAccount> createAccounts() {
return new PriorityQueue<IAccount>();
}
@Override
public IEmployee getEmployee(Collection<IEmployee> emps, int idx) {
if (emps == null || emps.iterator() == null) {
return null;
}
return emps.iterator().next();
}
@Override
public Product getProduct(Collection<Product> products, int idx) {
if (products == null || products.iterator() == null) {
return null;
}
return products.iterator().next();
}
}

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.proxy.delayed.pqueue;
import java.util.Collection;
import javax.persistence.EntityManager;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.util.DelayedPriorityQueueProxy;
public class TestDelayedPriorityQueueProxyDetachLite extends TestDelayedPriorityQueueProxy {
@Override
public void setUp() {
super.setUp(
"openjpa.DetachState", "loaded(LiteAutoDetach=true,detachProxyFields=false)");
}
/*
* Verify that a collection can be loaded post detachment
*/
@Override
public void testPostDetach() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
resetSQL();
em.clear();
d = findDepartment(em, d.getId());
emps = d.getEmployees();
em.close();
// assert there was no select on the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedPriorityQueueProxy);
DelayedPriorityQueueProxy dep = (DelayedPriorityQueueProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, dep.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// call contains and assert a select from the employee table
// occurred that the expected entities are returned.
resetSQL();
assertTrue(emps.contains(e));
e = getEmployee(emps,0);
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
resetSQL();
assertEquals(1, emps.size());
// Verify the delay load entity is detached
assertTrue(e instanceof PersistenceCapable);
PersistenceCapable pc = (PersistenceCapable)e;
// LiteAutoDetach
assertTrue(pc.pcGetStateManager() == null);
// verify a second SQL was not issued to get the size
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.proxy.delayed.pqueue;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_UIDENT")
public class UserIdentity implements IUserIdentity {
@Id
@GeneratedValue
@Column(name="UID_ID")
private int id;
@Embedded
private Member member;
public void setMember(IMember member) {
this.member = (Member)member;
}
public IMember getMember() {
return member;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}

View File

@ -0,0 +1,84 @@
/*
* 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.proxy.delayed.tset;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_ACCOUNT")
public class Account implements IAccount, Comparable<Account> {
public Account() {
}
public Account(String name, IUserIdentity uid) {
setName(name);
setUserIdent(uid);
}
@Id
@GeneratedValue
@Column(name="ACCT_ID")
private int id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="UID_ID")
private UserIdentity userIdent;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setUserIdent(IUserIdentity userIdent) {
this.userIdent = (UserIdentity)userIdent;
}
public IUserIdentity getUserIdent() {
return userIdent;
}
@Override
public int compareTo(Account o) {
return new Integer(getId()).compareTo(o.getId());
}
}

View File

@ -0,0 +1,135 @@
/*
* 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.proxy.delayed.tset;
import java.io.Serializable;
import java.util.Collection;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
@Entity
@Table(name="DC_DEPARTMENT")
public class Department implements IDepartment, Serializable {
private static final long serialVersionUID = -6923551949033215888L;
@Id
@GeneratedValue
private int id;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY, targetEntity=Employee.class)
@JoinTable(name="DC_DEP_EMP")
private TreeSet<IEmployee> employees;
@OrderColumn
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
@JoinTable(name="DC_DEP_LOC")
private TreeSet<Location> locations;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(name="DC_DEP_PRD")
private SortedSet<Product> products;
@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name="DC_DEP_CERT")
private TreeSet<Certification> certifications;
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name="DC_DEP_AWD")
private SortedSet<Award> awards;
@Override
public void setEmployees(Collection<IEmployee> employees) {
this.employees = (TreeSet<IEmployee>)employees;
}
@Override
public Collection<IEmployee> getEmployees() {
return employees;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public int getId() {
return id;
}
@Override
public void setLocations(Collection<Location> locations) {
this.locations =(TreeSet<Location>)locations;
}
@Override
public Collection<Location> getLocations() {
return locations;
}
@Override
public void setProducts(Collection<Product> products) {
this.products = (TreeSet<Product>)products;
}
@Override
public Collection<Product> getProducts() {
return products;
}
@Override
public void setCertifications(Collection<Certification> certifications) {
this.certifications = (TreeSet<Certification>)certifications;
}
@Override
public Collection<Certification> getCertifications() {
return certifications;
}
@Override
public void setAwards(Collection<Award> awards) {
this.awards = (TreeSet<Award>)awards;
}
@Override
public Collection<Award> getAwards() {
return awards;
}
}

View File

@ -0,0 +1,96 @@
/*
* 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.proxy.delayed.tset;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
@Entity
@Table(name="DC_EMPLOYEE")
public class Employee implements IEmployee, Serializable, Comparable<Employee> {
private static final long serialVersionUID = 1878272252981151246L;
@Id
@GeneratedValue
private int id;
private String empName;
@ManyToOne(targetEntity=Department.class)
@JoinColumn(name="DEPT_ID")
private IDepartment dept;
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpName() {
return empName;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setDept(IDepartment dept) {
this.dept = dept;
}
public IDepartment getDept() {
return dept;
}
@Override
public int hashCode() {
return getId();
}
public boolean equals(Object obj) {
if (obj instanceof Employee) {
Employee e = (Employee)obj;
return e.getId() == getId() && e.getEmpName().equals(getEmpName());
}
return false;
}
@Override
public int compareTo(Employee e) {
String nameId = getEmpName() + getId();
String nameId2 = e.getEmpName() + e.getId();
return nameId.compareTo(nameId2);
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.proxy.delayed.tset;
import java.util.Collection;
import java.util.PriorityQueue;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
@Embeddable
public class Member implements IMember {
private String name;
@OneToMany(fetch=FetchType.LAZY, mappedBy="userIdent", targetEntity=Account.class)
private PriorityQueue<IAccount> accounts;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAccounts(Collection<IAccount> accounts) {
this.accounts = (PriorityQueue<IAccount>)accounts;
}
public Collection<IAccount> getAccounts() {
return accounts;
}
}

View File

@ -0,0 +1,143 @@
/*
* 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.proxy.delayed.tset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.DelayedProxyCollectionsTestCase;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
public class TestDelayedTreeSetProxy extends DelayedProxyCollectionsTestCase {
public static Object[] _pcList = {
Employee.class,
Department.class,
UserIdentity.class,
Member.class,
Account.class
};
public void setUp() {
super.setUp(_pcList);
}
public void setUp(Object... props){
List<Object> parms = new ArrayList<Object>();
// Add package-specific types
parms.addAll(Arrays.asList(_pcList));
// Add properties from super
parms.addAll(Arrays.asList(props));
super.setUp(parms.toArray());
}
public IUserIdentity findUserIdentity(EntityManager em, int id) {
return em.find(UserIdentity.class, id);
}
public IDepartment findDepartment(EntityManager em, int id) {
return em.find(Department.class, id);
}
public IUserIdentity createUserIdentity() {
UserIdentity ui = new UserIdentity();
return ui;
}
public IAccount createAccount(String name, IUserIdentity ui) {
IAccount acct = new Account(name, ui);
return acct;
}
public IDepartment createDepartment() {
Department d = new Department();
return d;
}
public IMember createMember(String name) {
Member m = new Member();
m.setName(name);
return m;
}
@Override
public IEmployee createEmployee() {
Employee e = new Employee();
return e;
}
@Override
public Collection<IEmployee> createEmployees() {
return new TreeSet<IEmployee>();
}
@Override
public Collection<Product> createProducts() {
return new TreeSet<Product>();
}
@Override
public Collection<Award> createAwards() {
return new TreeSet<Award>();
}
@Override
public Collection<Location> createLocations() {
return new TreeSet<Location>();
}
@Override
public Collection<Certification> createCertifications() {
return new TreeSet<Certification>();
}
@Override
public Collection<IAccount> createAccounts() {
return new TreeSet<IAccount>();
}
@Override
public IEmployee getEmployee(Collection<IEmployee> emps, int idx) {
if (emps == null || emps.iterator() == null) {
return null;
}
return emps.iterator().next();
}
@Override
public Product getProduct(Collection<Product> products, int idx) {
if (products == null || products.iterator() == null) {
return null;
}
return products.iterator().next();
}
}

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.proxy.delayed.tset;
import java.util.Collection;
import javax.persistence.EntityManager;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.util.DelayedTreeSetProxy;
public class TestDelayedTreeSetProxyDetachLite extends TestDelayedTreeSetProxy {
@Override
public void setUp() {
super.setUp(
"openjpa.DetachState", "loaded(LiteAutoDetach=true,detachProxyFields=false)");
}
/*
* Verify that a collection can be loaded post detachment
*/
@Override
public void testPostDetach() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
resetSQL();
em.clear();
d = findDepartment(em, d.getId());
emps = d.getEmployees();
em.close();
// assert there was no select on the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedTreeSetProxy);
DelayedTreeSetProxy dep = (DelayedTreeSetProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, dep.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// call contains and assert a select from the employee table
// occurred that the expected entities are returned.
resetSQL();
assertTrue(emps.contains(e));
e = getEmployee(emps,0);
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
resetSQL();
assertEquals(1, emps.size());
// Verify the delay load entity is detached
assertTrue(e instanceof PersistenceCapable);
PersistenceCapable pc = (PersistenceCapable)e;
// LiteAutoDetach
assertTrue(pc.pcGetStateManager() == null);
// verify a second SQL was not issued to get the size
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.proxy.delayed.tset;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_UIDENT")
public class UserIdentity implements IUserIdentity {
@Id
@GeneratedValue
@Column(name="UID_ID")
private int id;
@Embedded
private Member member;
public void setMember(IMember member) {
this.member = (Member)member;
}
public IMember getMember() {
return member;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}

View File

@ -0,0 +1,79 @@
/*
* 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.proxy.delayed.vec;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_ACCOUNT")
public class Account implements IAccount {
public Account() {
}
public Account(String name, IUserIdentity uid) {
setName(name);
setUserIdent(uid);
}
@Id
@GeneratedValue
@Column(name="ACCT_ID")
private int id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="UID_ID")
private UserIdentity userIdent;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setUserIdent(IUserIdentity userIdent) {
this.userIdent = (UserIdentity)userIdent;
}
public IUserIdentity getUserIdent() {
return userIdent;
}
}

View File

@ -0,0 +1,134 @@
/*
* 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.proxy.delayed.vec;
import java.io.Serializable;
import java.util.Collection;
import java.util.Vector;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
@Entity
@Table(name="DC_DEPARTMENT")
public class Department implements IDepartment, Serializable {
private static final long serialVersionUID = -6923551949033215888L;
@Id
@GeneratedValue
private int id;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY, targetEntity=Employee.class)
@JoinTable(name="DC_DEP_EMP")
private Vector<IEmployee> employees;
@OrderColumn
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
@JoinTable(name="DC_DEP_LOC")
private Vector<Location> locations;
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(name="DC_DEP_PRD")
private Vector<Product> products;
@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name="DC_DEP_CERT")
private Vector<Certification> certifications;
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name="DC_DEP_AWD")
private Vector<Award> awards;
@Override
public void setEmployees(Collection<IEmployee> employees) {
this.employees = (Vector<IEmployee>)employees;
}
@Override
public Collection<IEmployee> getEmployees() {
return employees;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public int getId() {
return id;
}
@Override
public void setLocations(Collection<Location> locations) {
this.locations =(Vector<Location>)locations;
}
@Override
public Collection<Location> getLocations() {
return locations;
}
@Override
public void setProducts(Collection<Product> products) {
this.products = (Vector<Product>)products;
}
@Override
public Collection<Product> getProducts() {
return products;
}
@Override
public void setCertifications(Collection<Certification> certifications) {
this.certifications = (Vector<Certification>)certifications;
}
@Override
public Collection<Certification> getCertifications() {
return certifications;
}
@Override
public void setAwards(Collection<Award> awards) {
this.awards = (Vector<Award>)awards;
}
@Override
public Collection<Award> getAwards() {
return awards;
}
}

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.proxy.delayed.vec;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
@Entity
@Table(name="DC_EMPLOYEE")
public class Employee implements IEmployee, Serializable {
private static final long serialVersionUID = 1878272252981151246L;
@Id
@GeneratedValue
private int id;
private String empName;
@ManyToOne(targetEntity=Department.class)
@JoinColumn(name="DEPT_ID")
private IDepartment dept;
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpName() {
return empName;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setDept(IDepartment dept) {
this.dept = dept;
}
public IDepartment getDept() {
return dept;
}
@Override
public int hashCode() {
return getId();
}
public boolean equals(Object obj) {
if (obj instanceof Employee) {
Employee e = (Employee)obj;
return e.getId() == getId() && e.getEmpName().equals(getEmpName());
}
return false;
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.proxy.delayed.vec;
import java.util.Collection;
import java.util.Vector;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
@Embeddable
public class Member implements IMember {
private String name;
@OneToMany(fetch=FetchType.LAZY, mappedBy="userIdent", targetEntity=Account.class)
private Vector<IAccount> accounts;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAccounts(Collection<IAccount> accounts) {
this.accounts = (Vector<IAccount>)accounts;
}
public Collection<IAccount> getAccounts() {
return accounts;
}
}

View File

@ -0,0 +1,137 @@
/*
* 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.proxy.delayed.vec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Vector;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.proxy.delayed.Award;
import org.apache.openjpa.persistence.proxy.delayed.Certification;
import org.apache.openjpa.persistence.proxy.delayed.DelayedProxyCollectionsTestCase;
import org.apache.openjpa.persistence.proxy.delayed.IAccount;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
import org.apache.openjpa.persistence.proxy.delayed.Location;
import org.apache.openjpa.persistence.proxy.delayed.Product;
public class TestDelayedVectorProxy extends DelayedProxyCollectionsTestCase {
public static Object[] _pcList = {
Employee.class,
Department.class,
UserIdentity.class,
Member.class,
Account.class
};
public void setUp() {
super.setUp(_pcList);
}
public void setUp(Object... props){
List<Object> parms = new ArrayList<Object>();
// Add package-specific types
parms.addAll(Arrays.asList(_pcList));
// Add properties from super
parms.addAll(Arrays.asList(props));
super.setUp(parms.toArray());
}
public IUserIdentity findUserIdentity(EntityManager em, int id) {
return em.find(UserIdentity.class, id);
}
public IDepartment findDepartment(EntityManager em, int id) {
return em.find(Department.class, id);
}
public IUserIdentity createUserIdentity() {
UserIdentity ui = new UserIdentity();
return ui;
}
public IAccount createAccount(String name, IUserIdentity ui) {
IAccount acct = new Account(name, ui);
return acct;
}
public IDepartment createDepartment() {
Department d = new Department();
return d;
}
public IMember createMember(String name) {
Member m = new Member();
m.setName(name);
return m;
}
@Override
public IEmployee createEmployee() {
Employee e = new Employee();
return e;
}
@Override
public Collection<IEmployee> createEmployees() {
return new Vector<IEmployee>();
}
@Override
public Collection<Product> createProducts() {
return new Vector<Product>();
}
@Override
public Collection<Award> createAwards() {
return new Vector<Award>();
}
@Override
public Collection<Location> createLocations() {
return new Vector<Location>();
}
@Override
public Collection<Certification> createCertifications() {
return new Vector<Certification>();
}
@Override
public Collection<IAccount> createAccounts() {
return new Vector<IAccount>();
}
@Override
public IEmployee getEmployee(Collection<IEmployee> emps, int idx) {
return ((Vector<IEmployee>)emps).get(idx);
}
@Override
public Product getProduct(Collection<Product> products, int idx) {
return ((Vector<Product>)products).get(idx);
}
}

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.proxy.delayed.vec;
import java.util.Collection;
import javax.persistence.EntityManager;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.persistence.proxy.delayed.IDepartment;
import org.apache.openjpa.persistence.proxy.delayed.IEmployee;
import org.apache.openjpa.util.DelayedVectorProxy;
public class TestDelayedVectorProxyDetachLite extends TestDelayedVectorProxy {
@Override
public void setUp() {
super.setUp(
"openjpa.DetachState", "loaded(LiteAutoDetach=true,detachProxyFields=false)");
}
/*
* Verify that a collection can be loaded post detachment
*/
@Override
public void testPostDetach() {
EntityManager em = emf.createEntityManager();
// Create a new department and an employee
IDepartment d = createDepartment();
IEmployee e = createEmployee();
e.setDept(d);
e.setEmpName("John");
Collection<IEmployee> emps = createEmployees();
emps.add(e);
d.setEmployees(emps);
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
resetSQL();
em.clear();
d = findDepartment(em, d.getId());
emps = d.getEmployees();
em.close();
// assert there was no select on the employee table
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
assertTrue(emps instanceof DelayedVectorProxy);
DelayedVectorProxy dep = (DelayedVectorProxy)emps;
dep.setDirectAccess(true);
assertEquals(0, dep.size());
dep.setDirectAccess(false);
assertNotNull(emps);
// call contains and assert a select from the employee table
// occurred that the expected entities are returned.
resetSQL();
assertTrue(emps.contains(e));
e = getEmployee(emps,0);
assertAnySQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
resetSQL();
assertEquals(1, emps.size());
// Verify the delay load entity is detached
assertTrue(e instanceof PersistenceCapable);
PersistenceCapable pc = (PersistenceCapable)e;
// LiteAutoDetach
assertTrue(pc.pcGetStateManager() == null);
// verify a second SQL was not issued to get the size
assertNoneSQLAnyOrder("SELECT .* DC_EMPLOYEE .*");
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.proxy.delayed.vec;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.openjpa.persistence.proxy.delayed.IMember;
import org.apache.openjpa.persistence.proxy.delayed.IUserIdentity;
@Entity
@Table(name="DC_UIDENT")
public class UserIdentity implements IUserIdentity {
@Id
@GeneratedValue
@Column(name="UID_ID")
private int id;
@Embedded
private Member member;
public void setMember(IMember member) {
this.member = (Member)member;
}
public IMember getMember() {
return member;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}