You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pc...@apache.org on 2008/04/28 01:12:16 UTC
svn commit: r652026 - in /openjpa/branches/1.1.x:
openjpa-kernel/src/main/java/org/apache/openjpa/kernel/
openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fetchgroups/
op...
Author: pcl
Date: Sun Apr 27 16:12:10 2008
New Revision: 652026
URL: http://svn.apache.org/viewvc?rev=652026&view=rev
Log:
OPENJPA-522
Added:
openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fetchgroups/TestFetchGroupStacks.java
Modified:
openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBroker.java
openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StoreContext.java
openjpa/branches/1.1.x/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties
openjpa/branches/1.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
openjpa/branches/1.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAEntityManager.java
Modified: openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?rev=652026&r1=652025&r2=652026&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java (original)
+++ openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java Sun Apr 27 16:12:10 2008
@@ -237,6 +237,7 @@
private int _lifeCallbackMode = 0;
private transient boolean _initializeWasInvoked = false;
+ private LinkedList _fcs;
/**
* Set the persistence manager's authentication. This is the first
@@ -383,6 +384,20 @@
public FetchConfiguration getFetchConfiguration() {
return _fc;
+ }
+
+ public FetchConfiguration pushFetchConfiguration() {
+ if (_fcs == null)
+ _fcs = new LinkedList();
+ _fcs.add(_fc);
+ _fc = (FetchConfiguration) _fc.clone();
+ return _fc;
+ }
+
+ public void popFetchConfiguration() {
+ if (_fcs == null || _fcs.isEmpty())
+ throw new UserException(_loc.get("fetch-configuration-stack-empty"));
+ _fc = (FetchConfiguration) _fcs.removeLast();
}
public int getConnectionRetainMode() {
Modified: openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBroker.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBroker.java?rev=652026&r1=652025&r2=652026&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBroker.java (original)
+++ openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/DelegatingBroker.java Sun Apr 27 16:12:10 2008
@@ -122,6 +122,22 @@
}
}
+ public FetchConfiguration pushFetchConfiguration() {
+ try {
+ return _broker.pushFetchConfiguration();
+ } catch (RuntimeException re) {
+ throw translate(re);
+ }
+ }
+
+ public void popFetchConfiguration() {
+ try {
+ _broker.popFetchConfiguration();
+ } catch (RuntimeException re) {
+ throw translate(re);
+ }
+ }
+
public ClassLoader getClassLoader() {
try {
return _broker.getClassLoader();
Modified: openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StoreContext.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StoreContext.java?rev=652026&r1=652025&r2=652026&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StoreContext.java (original)
+++ openjpa/branches/1.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StoreContext.java Sun Apr 27 16:12:10 2008
@@ -62,6 +62,27 @@
public FetchConfiguration getFetchConfiguration();
/**
+ * Pushes a new fetch configuration that inherits from the current
+ * fetch configuration onto a stack, and makes the new configuration
+ * the active one.
+ *
+ * @since 1.1.0
+ * @return the new fetch configuration
+ */
+ public FetchConfiguration pushFetchConfiguration();
+
+ /**
+ * Pops the fetch configuration from the top of the stack, making the
+ * next one down the active one. This returns void to avoid confusion,
+ * since fetch configurations tend to be used in method-chaining
+ * patterns often.
+ *
+ * @since 1.1.0
+ * @throws UserException if the fetch configuration stack is empty
+ */
+ public void popFetchConfiguration();
+
+ /**
* Return the current thread's class loader at the time this context
* was obtained.
*/
Modified: openjpa/branches/1.1.x/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties?rev=652026&r1=652025&r2=652026&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties (original)
+++ openjpa/branches/1.1.x/openjpa-kernel/src/main/resources/org/apache/openjpa/kernel/localizer.properties Sun Apr 27 16:12:10 2008
@@ -399,3 +399,4 @@
cant-serialize-connected-broker: Serialization not allowed for brokers with \
an active connection to the database.
no-interface-metadata: No metadata was found for managed interface {0}.
+fetch-configuration-stack-empty: Fetch configuration stack is empty.
Added: openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fetchgroups/TestFetchGroupStacks.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fetchgroups/TestFetchGroupStacks.java?rev=652026&view=auto
==============================================================================
--- openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fetchgroups/TestFetchGroupStacks.java (added)
+++ openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fetchgroups/TestFetchGroupStacks.java Sun Apr 27 16:12:10 2008
@@ -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.fetchgroups;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.openjpa.persistence.test.SingleEMTestCase;
+import org.apache.openjpa.persistence.PersistenceException;
+
+public class TestFetchGroupStacks extends SingleEMTestCase {
+
+ public void setUp() {
+ setUp(FGManager.class, FGDepartment.class, FGEmployee.class,
+ FGAddress.class);
+ }
+
+ public void testFetchGroupStacks() {
+ assertFetchGroups();
+ em.getFetchPlan().addFetchGroup("foo");
+ assertFetchGroups("foo");
+
+ { // add one new fetch group
+ em.pushFetchPlan().addFetchGroup("bar"); // push 1
+ assertFetchGroups("foo", "bar");
+
+ { // add another one
+ em.pushFetchPlan().addFetchGroup("baz"); // push 2
+ assertFetchGroups("foo", "bar", "baz");
+
+ { // add a fourth, plus one that's already there
+ em.pushFetchPlan().addFetchGroups("quux", "foo"); // push 3
+ assertFetchGroups("foo", "bar", "baz", "quux");
+ em.popFetchPlan(); // pop 3
+ }
+
+ // "foo" should still be there, since it was there before pop 3
+ assertFetchGroups("foo", "bar", "baz");
+ em.popFetchPlan(); // pop 2
+ }
+
+ assertFetchGroups("foo", "bar");
+ em.popFetchPlan(); // pop 1
+ }
+ assertFetchGroups("foo");
+
+ try {
+ em.popFetchPlan();
+ fail("should be unbalanced");
+ } catch (RuntimeException e) {
+ assertTrue(e.getMessage().contains("stack"));
+ }
+ }
+
+ private void assertFetchGroups(String... fgs) {
+ Set s = new HashSet();
+ if (fgs != null)
+ s.addAll(Arrays.asList(fgs));
+ s.add("default");
+ assertEquals(s, em.getFetchPlan().getFetchGroups());
+ }
+}
Modified: openjpa/branches/1.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java?rev=652026&r1=652025&r2=652026&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java (original)
+++ openjpa/branches/1.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java Sun Apr 27 16:12:10 2008
@@ -31,6 +31,8 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
+import java.util.Map;
+import java.util.HashMap;
import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
@@ -51,6 +53,7 @@
import org.apache.openjpa.kernel.QueryFlushModes;
import org.apache.openjpa.kernel.QueryLanguages;
import org.apache.openjpa.kernel.Seq;
+import org.apache.openjpa.kernel.FetchConfiguration;
import org.apache.openjpa.kernel.jpql.JPQLParser;
import org.apache.openjpa.lib.util.Closeable;
import org.apache.openjpa.lib.util.Localizer;
@@ -80,7 +83,8 @@
private DelegatingBroker _broker;
private EntityManagerFactoryImpl _emf;
- private FetchPlan _fetch = null;
+ private Map<FetchConfiguration,FetchPlan> _plans =
+ new HashMap<FetchConfiguration,FetchPlan>(1);
private RuntimeExceptionTranslator ret =
PersistenceExceptions.getRollbackTranslator(this);
@@ -122,10 +126,34 @@
assertNotCloseInvoked();
_broker.lock();
try {
- if (_fetch == null)
- _fetch = _emf.toFetchPlan(_broker,
- _broker.getFetchConfiguration());
- return _fetch;
+ FetchConfiguration fc = _broker.getFetchConfiguration();
+ FetchPlan fp = _plans.get(fc);
+ if (fp == null) {
+ fp = _emf.toFetchPlan(_broker, fc);
+ _plans.put(fc, fp);
+ }
+ return fp;
+ } finally {
+ _broker.unlock();
+ }
+ }
+
+ public FetchPlan pushFetchPlan() {
+ assertNotCloseInvoked();
+ _broker.lock();
+ try {
+ _broker.pushFetchConfiguration();
+ return getFetchPlan();
+ } finally {
+ _broker.unlock();
+ }
+ }
+
+ public void popFetchPlan() {
+ assertNotCloseInvoked();
+ _broker.lock();
+ try {
+ _broker.popFetchConfiguration();
} finally {
_broker.unlock();
}
Modified: openjpa/branches/1.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAEntityManager.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAEntityManager.java?rev=652026&r1=652025&r2=652026&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAEntityManager.java (original)
+++ openjpa/branches/1.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAEntityManager.java Sun Apr 27 16:12:10 2008
@@ -57,6 +57,24 @@
public FetchPlan getFetchPlan();
/**
+ * Pushes a new fetch plan that inherits from the current fetch plan onto
+ * a stack, and makes the new plan the active one.
+ *
+ * @since 1.1.0
+ * @return the new fetch plan
+ */
+ public FetchPlan pushFetchPlan();
+
+ /**
+ * Pops the fetch plan from the top of the stack, making the next one down
+ * the active one. This returns void to avoid confusion, since fetch plans
+ * tend to be used in method-chaining patterns often.
+ *
+ * @since 1.1.0
+ */
+ public void popFetchPlan();
+
+ /**
* Return the connection retain mode for this entity manager.
*/
public ConnectionRetainMode getConnectionRetainMode();