21 changed files with 94 additions and 2695 deletions
@ -1,172 +0,0 @@
@@ -1,172 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed 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.springframework.orm.jdo; |
||||
|
||||
import javax.jdo.JDOException; |
||||
import javax.jdo.PersistenceManager; |
||||
import javax.jdo.PersistenceManagerFactory; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.beans.factory.InitializingBean; |
||||
import org.springframework.dao.DataAccessException; |
||||
|
||||
/** |
||||
* Base class for JdoTemplate and JdoInterceptor, defining common |
||||
* properties such as PersistenceManagerFactory and flushing behavior. |
||||
* |
||||
* <p>Note: With JDO, modifications to persistent objects are just possible |
||||
* within a transaction (in contrast to Hibernate). Therefore, eager flushing |
||||
* will just get applied when in a transaction. Furthermore, there is no |
||||
* explicit notion of flushing never, as this would not imply a performance |
||||
* gain due to JDO's field interception mechanism (which doesn't involve |
||||
* the overhead of snapshot comparisons). |
||||
* |
||||
* <p>Eager flushing is just available for specific JDO providers. |
||||
* You need to a corresponding JdoDialect to make eager flushing work. |
||||
* |
||||
* <p>Not intended to be used directly. See JdoTemplate and JdoInterceptor. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 02.11.2003 |
||||
* @see JdoTemplate |
||||
* @see JdoInterceptor |
||||
* @see #setFlushEager |
||||
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage |
||||
* (see {@link TransactionAwarePersistenceManagerFactoryProxy} and |
||||
* {@link org.springframework.orm.jdo.support.SpringPersistenceManagerProxyBean}) |
||||
*/ |
||||
@Deprecated |
||||
public abstract class JdoAccessor implements InitializingBean { |
||||
|
||||
/** Logger available to subclasses */ |
||||
protected final Log logger = LogFactory.getLog(getClass()); |
||||
|
||||
private PersistenceManagerFactory persistenceManagerFactory; |
||||
|
||||
private JdoDialect jdoDialect; |
||||
|
||||
private boolean flushEager = false; |
||||
|
||||
|
||||
/** |
||||
* Set the JDO PersistenceManagerFactory that should be used to create |
||||
* PersistenceManagers. |
||||
*/ |
||||
public void setPersistenceManagerFactory(PersistenceManagerFactory pmf) { |
||||
this.persistenceManagerFactory = pmf; |
||||
} |
||||
|
||||
/** |
||||
* Return the JDO PersistenceManagerFactory that should be used to create |
||||
* PersistenceManagers. |
||||
*/ |
||||
public PersistenceManagerFactory getPersistenceManagerFactory() { |
||||
return this.persistenceManagerFactory; |
||||
} |
||||
|
||||
/** |
||||
* Set the JDO dialect to use for this accessor. |
||||
* <p>The dialect object can be used to retrieve the underlying JDBC |
||||
* connection and to eagerly flush changes to the database. |
||||
* <p>Default is a DefaultJdoDialect based on the PersistenceManagerFactory's |
||||
* underlying DataSource, if any. |
||||
*/ |
||||
public void setJdoDialect(JdoDialect jdoDialect) { |
||||
this.jdoDialect = jdoDialect; |
||||
} |
||||
|
||||
/** |
||||
* Return the JDO dialect to use for this accessor. |
||||
* <p>Creates a default one for the specified PersistenceManagerFactory if none set. |
||||
*/ |
||||
public JdoDialect getJdoDialect() { |
||||
if (this.jdoDialect == null) { |
||||
this.jdoDialect = new DefaultJdoDialect(); |
||||
} |
||||
return this.jdoDialect; |
||||
} |
||||
|
||||
/** |
||||
* Set if this accessor should flush changes to the database eagerly. |
||||
* <p>Eager flushing leads to immediate synchronization with the database, |
||||
* even if in a transaction. This causes inconsistencies to show up and throw |
||||
* a respective exception immediately, and JDBC access code that participates |
||||
* in the same transaction will see the changes as the database is already |
||||
* aware of them then. But the drawbacks are: |
||||
* <ul> |
||||
* <li>additional communication roundtrips with the database, instead of a |
||||
* single batch at transaction commit; |
||||
* <li>the fact that an actual database rollback is needed if the JDO |
||||
* transaction rolls back (due to already submitted SQL statements). |
||||
* </ul> |
||||
*/ |
||||
public void setFlushEager(boolean flushEager) { |
||||
this.flushEager = flushEager; |
||||
} |
||||
|
||||
/** |
||||
* Return if this accessor should flush changes to the database eagerly. |
||||
*/ |
||||
public boolean isFlushEager() { |
||||
return this.flushEager; |
||||
} |
||||
|
||||
/** |
||||
* Eagerly initialize the JDO dialect, creating a default one |
||||
* for the specified PersistenceManagerFactory if none set. |
||||
*/ |
||||
public void afterPropertiesSet() { |
||||
if (getPersistenceManagerFactory() == null) { |
||||
throw new IllegalArgumentException("Property 'persistenceManagerFactory' is required"); |
||||
} |
||||
// Build default JdoDialect if none explicitly specified.
|
||||
if (this.jdoDialect == null) { |
||||
this.jdoDialect = new DefaultJdoDialect(getPersistenceManagerFactory().getConnectionFactory()); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Flush the given JDO persistence manager if necessary. |
||||
* @param pm the current JDO PersistenceManager |
||||
* @param existingTransaction if executing within an existing transaction |
||||
* (within an existing JDO PersistenceManager that won't be closed immediately) |
||||
* @throws JDOException in case of JDO flushing errors |
||||
*/ |
||||
protected void flushIfNecessary(PersistenceManager pm, boolean existingTransaction) throws JDOException { |
||||
if (isFlushEager()) { |
||||
logger.debug("Eagerly flushing JDO persistence manager"); |
||||
pm.flush(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Convert the given JDOException to an appropriate exception from the |
||||
* {@code org.springframework.dao} hierarchy. |
||||
* <p>Default implementation delegates to the JdoDialect. |
||||
* May be overridden in subclasses. |
||||
* @param ex JDOException that occured |
||||
* @return the corresponding DataAccessException instance |
||||
* @see JdoDialect#translateException |
||||
*/ |
||||
public DataAccessException convertJdoAccessException(JDOException ex) { |
||||
return getJdoDialect().translateException(ex); |
||||
} |
||||
|
||||
} |
||||
@ -1,73 +0,0 @@
@@ -1,73 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed 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.springframework.orm.jdo; |
||||
|
||||
import javax.jdo.JDOException; |
||||
import javax.jdo.PersistenceManager; |
||||
|
||||
/** |
||||
* Callback interface for JDO code. To be used with {@link JdoTemplate}'s |
||||
* execution methods, often as anonymous classes within a method implementation. |
||||
* A typical implementation will call PersistenceManager CRUD to perform |
||||
* some operations on persistent objects. |
||||
* |
||||
* <p>Note that JDO works on bytecode-modified Java objects, to be able to |
||||
* perform dirty detection on each modification of a persistent instance field. |
||||
* In contrast to Hibernate, using returned objects outside of an active |
||||
* PersistenceManager poses a problem: To be able to read and modify fields |
||||
* e.g. in a web GUI, one has to explicitly make the instances "transient". |
||||
* Reassociation with a new PersistenceManager, e.g. for updates when coming |
||||
* back from the GUI, isn't possible, as the JDO instances have lost their |
||||
* identity when turned transient. This means that either value objects have |
||||
* to be used as parameters, or the contents of the outside-modified instance |
||||
* have to be copied to a freshly loaded active instance on reassociation. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 03.06.2003 |
||||
* @see JdoTemplate |
||||
* @see JdoTransactionManager |
||||
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage |
||||
* (see {@link TransactionAwarePersistenceManagerFactoryProxy} and |
||||
* {@link org.springframework.orm.jdo.support.SpringPersistenceManagerProxyBean}) |
||||
*/ |
||||
@Deprecated |
||||
public interface JdoCallback<T> { |
||||
|
||||
/** |
||||
* Gets called by {@code JdoTemplate.execute} with an active JDO |
||||
* {@code PersistenceManager}. Does not need to care about activating |
||||
* or closing the {@code PersistenceManager}, or handling transactions. |
||||
* |
||||
* <p>Note that JDO callback code will not flush any modifications to the |
||||
* database if not executed within a transaction. Thus, you need to make |
||||
* sure that JdoTransactionManager has initiated a JDO transaction when |
||||
* the callback gets called, at least if you want to write to the database. |
||||
* |
||||
* <p>Allows for returning a result object created within the callback, |
||||
* i.e. a domain object or a collection of domain objects. |
||||
* A thrown custom RuntimeException is treated as an application exception: |
||||
* It gets propagated to the caller of the template. |
||||
* |
||||
* @param pm active PersistenceManager |
||||
* @return a result object, or {@code null} if none |
||||
* @throws JDOException if thrown by the JDO API |
||||
* @see JdoTemplate#execute |
||||
* @see JdoTemplate#executeFind |
||||
*/ |
||||
T doInJdo(PersistenceManager pm) throws JDOException; |
||||
|
||||
} |
||||
@ -1,128 +0,0 @@
@@ -1,128 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed 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.springframework.orm.jdo; |
||||
|
||||
import javax.jdo.JDOException; |
||||
import javax.jdo.PersistenceManager; |
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor; |
||||
import org.aopalliance.intercept.MethodInvocation; |
||||
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager; |
||||
|
||||
/** |
||||
* This interceptor binds a new JDO PersistenceManager to the thread before a method |
||||
* call, closing and removing it afterwards in case of any method outcome. |
||||
* If there already is a pre-bound PersistenceManager (e.g. from JdoTransactionManager, |
||||
* or from a surrounding JDO-intercepted method), the interceptor simply participates in it. |
||||
* |
||||
* <p>Application code must retrieve a JDO PersistenceManager via the |
||||
* {@code PersistenceManagerFactoryUtils.getPersistenceManager} method, |
||||
* to be able to detect a thread-bound PersistenceManager. It is preferable to use |
||||
* {@code getPersistenceManager} with allowCreate=false, if the code relies on |
||||
* the interceptor to provide proper PersistenceManager handling. Typically, the code |
||||
* will look like as follows: |
||||
* |
||||
* <pre> |
||||
* public void doSomeDataAccessAction() { |
||||
* PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(this.pmf, false); |
||||
* ... |
||||
* }</pre> |
||||
* |
||||
* <p>Note that this interceptor automatically translates JDOExceptions, via |
||||
* delegating to the {@code PersistenceManagerFactoryUtils.convertJdoAccessException} |
||||
* method that converts them to exceptions that are compatible with the |
||||
* {@code org.springframework.dao} exception hierarchy (like JdoTemplate does). |
||||
* This can be turned off if the raw exceptions are preferred. |
||||
* |
||||
* <p>This class can be considered a declarative alternative to JdoTemplate's |
||||
* callback approach. The advantages are: |
||||
* <ul> |
||||
* <li>no anonymous classes necessary for callback implementations; |
||||
* <li>the possibility to throw any application exceptions from within data access code. |
||||
* </ul> |
||||
* |
||||
* <p>The drawback is the dependency on interceptor configuration. However, note |
||||
* that this interceptor is usually <i>not</i> necessary in scenarios where the |
||||
* data access code always executes within transactions. A transaction will always |
||||
* have a thread-bound PersistenceManager in the first place, so adding this interceptor |
||||
* to the configuration just adds value when fine-tuning PersistenceManager settings |
||||
* like the flush mode - or when relying on exception translation. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 13.06.2003 |
||||
* @see PersistenceManagerFactoryUtils#getPersistenceManager |
||||
* @see JdoTransactionManager |
||||
* @see JdoTemplate |
||||
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage |
||||
* and AOP-driven exception translation through |
||||
* {@link org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor} |
||||
*/ |
||||
@Deprecated |
||||
public class JdoInterceptor extends JdoAccessor implements MethodInterceptor { |
||||
|
||||
private boolean exceptionConversionEnabled = true; |
||||
|
||||
|
||||
/** |
||||
* Set whether to convert any JDOException raised to a Spring DataAccessException, |
||||
* compatible with the {@code org.springframework.dao} exception hierarchy. |
||||
* <p>Default is "true". Turn this flag off to let the caller receive raw exceptions |
||||
* as-is, without any wrapping. |
||||
* @see org.springframework.dao.DataAccessException |
||||
*/ |
||||
public void setExceptionConversionEnabled(boolean exceptionConversionEnabled) { |
||||
this.exceptionConversionEnabled = exceptionConversionEnabled; |
||||
} |
||||
|
||||
|
||||
public Object invoke(MethodInvocation methodInvocation) throws Throwable { |
||||
boolean existingTransaction = false; |
||||
PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), true); |
||||
if (TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory())) { |
||||
logger.debug("Found thread-bound PersistenceManager for JDO interceptor"); |
||||
existingTransaction = true; |
||||
} |
||||
else { |
||||
logger.debug("Using new PersistenceManager for JDO interceptor"); |
||||
TransactionSynchronizationManager.bindResource(getPersistenceManagerFactory(), new PersistenceManagerHolder(pm)); |
||||
} |
||||
try { |
||||
Object retVal = methodInvocation.proceed(); |
||||
flushIfNecessary(pm, existingTransaction); |
||||
return retVal; |
||||
} |
||||
catch (JDOException ex) { |
||||
if (this.exceptionConversionEnabled) { |
||||
throw convertJdoAccessException(ex); |
||||
} |
||||
else { |
||||
throw ex; |
||||
} |
||||
} |
||||
finally { |
||||
if (existingTransaction) { |
||||
logger.debug("Not closing pre-bound JDO PersistenceManager after interceptor"); |
||||
} |
||||
else { |
||||
TransactionSynchronizationManager.unbindResource(getPersistenceManagerFactory()); |
||||
PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,420 +0,0 @@
@@ -1,420 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed 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.springframework.orm.jdo; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.Map; |
||||
|
||||
import org.springframework.dao.DataAccessException; |
||||
|
||||
/** |
||||
* Interface that specifies a basic set of JDO operations, |
||||
* implemented by {@link JdoTemplate}. Not often used, but a useful |
||||
* option to enhance testability, as it can easily be mocked or stubbed. |
||||
* |
||||
* <p>Defines {@code JdoTemplate}'s data access methods that mirror |
||||
* various JDO {@link javax.jdo.PersistenceManager} methods. Users are |
||||
* strongly encouraged to read the JDO {@code PersistenceManager} |
||||
* javadocs for details on the semantics of those methods. |
||||
* |
||||
* <p>Note that lazy loading will just work with an open JDO |
||||
* {@code PersistenceManager}, either within a managed transaction or within |
||||
* {@link org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter}/ |
||||
* {@link org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor}. |
||||
* Furthermore, some operations just make sense within transactions, |
||||
* for example: {@code evict}, {@code evictAll}, {@code flush}. |
||||
* |
||||
* <p>Updated to build on JDO 2.0 or higher, as of Spring 2.5. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 1.1 |
||||
* @see JdoTemplate |
||||
* @see javax.jdo.PersistenceManager |
||||
* @see JdoTransactionManager |
||||
* @see JdoDialect |
||||
* @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter |
||||
* @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor |
||||
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage |
||||
* (see {@link TransactionAwarePersistenceManagerFactoryProxy} and |
||||
* {@link org.springframework.orm.jdo.support.SpringPersistenceManagerProxyBean}) |
||||
*/ |
||||
@Deprecated |
||||
public interface JdoOperations { |
||||
|
||||
/** |
||||
* Execute the action specified by the given action object within a |
||||
* PersistenceManager. Application exceptions thrown by the action object |
||||
* get propagated to the caller (can only be unchecked). JDO exceptions |
||||
* are transformed into appropriate DAO ones. Allows for returning a |
||||
* result object, i.e. a domain object or a collection of domain objects. |
||||
* <p>Note: Callback code is not supposed to handle transactions itself! |
||||
* Use an appropriate transaction manager like JdoTransactionManager. |
||||
* @param action callback object that specifies the JDO action |
||||
* @return a result object returned by the action, or {@code null} |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see JdoTransactionManager |
||||
* @see javax.jdo.PersistenceManager |
||||
*/ |
||||
<T> T execute(JdoCallback<T> action) throws DataAccessException; |
||||
|
||||
/** |
||||
* Execute the specified action assuming that the result object is a |
||||
* Collection. This is a convenience method for executing JDO queries |
||||
* within an action. |
||||
* @param action callback object that specifies the JDO action |
||||
* @return a Collection result returned by the action, or {@code null} |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
*/ |
||||
Collection executeFind(JdoCallback<?> action) throws DataAccessException; |
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience methods for load, save, delete
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/** |
||||
* Return the persistent instance with the given JDO object id, |
||||
* throwing an exception if not found. |
||||
* <p>A JDO object id identifies both the persistent class and the id |
||||
* within the namespace of that class. |
||||
* @param objectId a JDO object id of the persistent instance |
||||
* @return the persistent instance |
||||
* @throws org.springframework.orm.ObjectRetrievalFailureException if not found |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#getObjectById(Object, boolean) |
||||
*/ |
||||
Object getObjectById(Object objectId) throws DataAccessException; |
||||
|
||||
/** |
||||
* Return the persistent instance of the given entity class
|
||||
* with the given id value, throwing an exception if not found. |
||||
* <p>The given id value is typically just unique within the namespace |
||||
* of the persistent class. Its toString value must correspond to the |
||||
* toString value of the corresponding JDO object id. |
||||
* <p>Usually, the passed-in value will have originated from the primary |
||||
* key field of a persistent object that uses JDO's application identity. |
||||
* @param entityClass a persistent class
|
||||
* @param idValue an id value of the persistent instance |
||||
* @return the persistent instance |
||||
* @throws org.springframework.orm.ObjectRetrievalFailureException if not found |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#getObjectById(Object, boolean) |
||||
* @see javax.jdo.PersistenceManager#getObjectById(Class, Object) |
||||
*/ |
||||
<T> T getObjectById(Class<T> entityClass, Object idValue) throws DataAccessException; |
||||
|
||||
/** |
||||
* Remove the given object from the PersistenceManager cache. |
||||
* @param entity the persistent instance to evict |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#evict(Object) |
||||
*/ |
||||
void evict(Object entity) throws DataAccessException; |
||||
|
||||
/** |
||||
* Remove all given objects from the PersistenceManager cache. |
||||
* @param entities the persistent instances to evict |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#evictAll(java.util.Collection) |
||||
*/ |
||||
void evictAll(Collection entities) throws DataAccessException; |
||||
|
||||
/** |
||||
* Remove all objects from the PersistenceManager cache. |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#evictAll() |
||||
*/ |
||||
void evictAll() throws DataAccessException; |
||||
|
||||
/** |
||||
* Re-read the state of the given persistent instance. |
||||
* @param entity the persistent instance to re-read |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#refresh(Object) |
||||
*/ |
||||
void refresh(Object entity) throws DataAccessException; |
||||
|
||||
/** |
||||
* Re-read the state of all given persistent instances. |
||||
* @param entities the persistent instances to re-read |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#refreshAll(java.util.Collection) |
||||
*/ |
||||
void refreshAll(Collection entities) throws DataAccessException; |
||||
|
||||
/** |
||||
* Re-read the state of all persistent instances. |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#refreshAll() |
||||
*/ |
||||
void refreshAll() throws DataAccessException; |
||||
|
||||
/** |
||||
* Make the given transient instance persistent. |
||||
* Attach the given entity if the instance is detached. |
||||
* @param entity the transient instance to make persistent |
||||
* @return the persistent instance |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#makePersistent(Object) |
||||
*/ |
||||
<T> T makePersistent(T entity) throws DataAccessException; |
||||
|
||||
/** |
||||
* Make the given transient instances persistent. |
||||
* Attach the given entities if the instances are detached. |
||||
* @param entities the transient instances to make persistent |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#makePersistentAll(java.util.Collection) |
||||
*/ |
||||
<T> Collection<T> makePersistentAll(Collection<T> entities) throws DataAccessException; |
||||
|
||||
/** |
||||
* Delete the given persistent instance. |
||||
* @param entity the persistent instance to delete |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#deletePersistent(Object) |
||||
*/ |
||||
void deletePersistent(Object entity) throws DataAccessException; |
||||
|
||||
/** |
||||
* Delete all given persistent instances. |
||||
* <p>This can be combined with any of the find methods to delete by query |
||||
* in two lines of code. |
||||
* @param entities the persistent instances to delete |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#deletePersistentAll(java.util.Collection) |
||||
*/ |
||||
void deletePersistentAll(Collection entities) throws DataAccessException; |
||||
|
||||
/** |
||||
* Detach a copy of the given persistent instance from the current JDO transaction, |
||||
* for use outside a JDO transaction (for example, as web form object). |
||||
* @param entity the persistent instance to detach |
||||
* @return the corresponding detached instance |
||||
* @see javax.jdo.PersistenceManager#detachCopy(Object) |
||||
*/ |
||||
<T> T detachCopy(T entity); |
||||
|
||||
/** |
||||
* Detach copies of the given persistent instances from the current JDO transaction, |
||||
* for use outside a JDO transaction (for example, as web form objects). |
||||
* @param entities the persistent instances to detach |
||||
* @return the corresponding detached instances |
||||
* @see javax.jdo.PersistenceManager#detachCopyAll(Collection) |
||||
*/ |
||||
<T> Collection<T> detachCopyAll(Collection<T> entities); |
||||
|
||||
/** |
||||
* Flush all transactional modifications to the database. |
||||
* <p>Only invoke this for selective eager flushing, for example when JDBC code |
||||
* needs to see certain changes within the same transaction. Else, it's preferable |
||||
* to rely on auto-flushing at transaction completion. |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#flush() |
||||
*/ |
||||
void flush() throws DataAccessException; |
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/** |
||||
* Find all persistent instances of the given class. |
||||
* @param entityClass a persistent class
|
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newQuery(Class) |
||||
*/ |
||||
<T> Collection<T> find(Class<T> entityClass) throws DataAccessException; |
||||
|
||||
/** |
||||
* Find all persistent instances of the given class that match the given |
||||
* JDOQL filter. |
||||
* @param entityClass a persistent class
|
||||
* @param filter the JDOQL filter to match (or {@code null} if none) |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newQuery(Class, String) |
||||
*/ |
||||
<T> Collection<T> find(Class<T> entityClass, String filter) throws DataAccessException; |
||||
|
||||
/** |
||||
* Find all persistent instances of the given class that match the given |
||||
* JDOQL filter, with the given result ordering. |
||||
* @param entityClass a persistent class
|
||||
* @param filter the JDOQL filter to match (or {@code null} if none) |
||||
* @param ordering the ordering of the result (or {@code null} if none) |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newQuery(Class, String) |
||||
* @see javax.jdo.Query#setOrdering |
||||
*/ |
||||
<T> Collection<T> find(Class<T> entityClass, String filter, String ordering) throws DataAccessException; |
||||
|
||||
/** |
||||
* Find all persistent instances of the given class that match the given |
||||
* JDOQL filter, using the given parameter declarations and parameter values. |
||||
* @param entityClass a persistent class
|
||||
* @param filter the JDOQL filter to match |
||||
* @param parameters the JDOQL parameter declarations |
||||
* @param values the corresponding parameter values |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newQuery(Class, String) |
||||
* @see javax.jdo.Query#declareParameters |
||||
* @see javax.jdo.Query#executeWithArray |
||||
*/ |
||||
<T> Collection<T> find(Class<T> entityClass, String filter, String parameters, Object... values) |
||||
throws DataAccessException; |
||||
|
||||
/** |
||||
* Find all persistent instances of the given class that match the given |
||||
* JDOQL filter, using the given parameter declarations and parameter values, |
||||
* with the given result ordering. |
||||
* @param entityClass a persistent class
|
||||
* @param filter the JDOQL filter to match |
||||
* @param parameters the JDOQL parameter declarations |
||||
* @param values the corresponding parameter values |
||||
* @param ordering the ordering of the result (or {@code null} if none) |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newQuery(Class, String) |
||||
* @see javax.jdo.Query#declareParameters |
||||
* @see javax.jdo.Query#executeWithArray |
||||
* @see javax.jdo.Query#setOrdering |
||||
*/ |
||||
<T> Collection<T> find( |
||||
Class<T> entityClass, String filter, String parameters, Object[] values, String ordering) |
||||
throws DataAccessException; |
||||
|
||||
/** |
||||
* Find all persistent instances of the given class that match the given |
||||
* JDOQL filter, using the given parameter declarations and parameter values. |
||||
* @param entityClass a persistent class
|
||||
* @param filter the JDOQL filter to match |
||||
* @param parameters the JDOQL parameter declarations |
||||
* @param values a Map with parameter names as keys and parameter values |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newQuery(Class, String) |
||||
* @see javax.jdo.Query#declareParameters |
||||
* @see javax.jdo.Query#executeWithMap |
||||
*/ |
||||
<T> Collection<T> find(Class<T> entityClass, String filter, String parameters, Map<String, ?> values) |
||||
throws DataAccessException; |
||||
|
||||
/** |
||||
* Find all persistent instances of the given class that match the given |
||||
* JDOQL filter, using the given parameter declarations and parameter values, |
||||
* with the given result ordering. |
||||
* @param entityClass a persistent class
|
||||
* @param filter the JDOQL filter to match |
||||
* @param parameters the JDOQL parameter declarations |
||||
* @param values a Map with parameter names as keys and parameter values |
||||
* @param ordering the ordering of the result (or {@code null} if none) |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newQuery(Class, String) |
||||
* @see javax.jdo.Query#declareParameters |
||||
* @see javax.jdo.Query#executeWithMap |
||||
* @see javax.jdo.Query#setOrdering |
||||
*/ |
||||
<T> Collection<T> find( |
||||
Class<T> entityClass, String filter, String parameters, Map<String, ?> values, String ordering) |
||||
throws DataAccessException; |
||||
|
||||
/** |
||||
* Find persistent instances through the given query object |
||||
* in the specified query language. |
||||
* @param language the query language ({@code javax.jdo.Query#JDOQL} |
||||
* or {@code javax.jdo.Query#SQL}, for example) |
||||
* @param queryObject the query object for the specified language |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newQuery(String, Object) |
||||
* @see javax.jdo.Query#JDOQL |
||||
* @see javax.jdo.Query#SQL |
||||
*/ |
||||
Collection find(String language, Object queryObject) throws DataAccessException; |
||||
|
||||
/** |
||||
* Find persistent instances through the given single-string JDOQL query. |
||||
* @param queryString the single-string JDOQL query |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newQuery(String) |
||||
*/ |
||||
Collection find(String queryString) throws DataAccessException; |
||||
|
||||
/** |
||||
* Find persistent instances through the given single-string JDOQL query. |
||||
* @param queryString the single-string JDOQL query |
||||
* @param values the corresponding parameter values |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newQuery(String) |
||||
*/ |
||||
Collection find(String queryString, Object... values) throws DataAccessException; |
||||
|
||||
/** |
||||
* Find persistent instances through the given single-string JDOQL query. |
||||
* @param queryString the single-string JDOQL query |
||||
* @param values a Map with parameter names as keys and parameter values |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newQuery(String) |
||||
*/ |
||||
Collection find(String queryString, Map<String, ?> values) throws DataAccessException; |
||||
|
||||
/** |
||||
* Find persistent instances through the given named query. |
||||
* @param entityClass a persistent class
|
||||
* @param queryName the name of the query |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newNamedQuery(Class, String) |
||||
*/ |
||||
<T> Collection<T> findByNamedQuery(Class<T> entityClass, String queryName) |
||||
throws DataAccessException; |
||||
|
||||
/** |
||||
* Find persistent instances through the given named query. |
||||
* @param entityClass a persistent class
|
||||
* @param queryName the name of the query |
||||
* @param values the corresponding parameter values |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newNamedQuery(Class, String) |
||||
*/ |
||||
<T> Collection<T> findByNamedQuery(Class<T> entityClass, String queryName, Object... values) |
||||
throws DataAccessException; |
||||
|
||||
/** |
||||
* Find persistent instances through the given named query. |
||||
* @param entityClass a persistent class
|
||||
* @param queryName the name of the query |
||||
* @param values a Map with parameter names as keys and parameter values |
||||
* @return the persistent instances |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
* @see javax.jdo.PersistenceManager#newNamedQuery(Class, String) |
||||
*/ |
||||
<T> Collection<T> findByNamedQuery(Class<T> entityClass, String queryName, Map<String, ?> values) |
||||
throws DataAccessException; |
||||
|
||||
} |
||||
@ -1,618 +0,0 @@
@@ -1,618 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed 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.springframework.orm.jdo; |
||||
|
||||
import java.lang.reflect.InvocationHandler; |
||||
import java.lang.reflect.InvocationTargetException; |
||||
import java.lang.reflect.Method; |
||||
import java.lang.reflect.Proxy; |
||||
import java.util.Collection; |
||||
import java.util.Map; |
||||
import javax.jdo.JDOException; |
||||
import javax.jdo.PersistenceManager; |
||||
import javax.jdo.PersistenceManagerFactory; |
||||
import javax.jdo.Query; |
||||
|
||||
import org.springframework.dao.DataAccessException; |
||||
import org.springframework.dao.InvalidDataAccessApiUsageException; |
||||
import org.springframework.transaction.support.TransactionSynchronizationManager; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ClassUtils; |
||||
|
||||
/** |
||||
* Helper class that simplifies JDO data access code, and converts |
||||
* JDOExceptions into Spring DataAccessExceptions, following the |
||||
* {@code org.springframework.dao} exception hierarchy. |
||||
* |
||||
* <p>The central method is {@code execute}, supporting JDO access code |
||||
* implementing the {@link JdoCallback} interface. It provides JDO PersistenceManager |
||||
* handling such that neither the JdoCallback implementation nor the calling |
||||
* code needs to explicitly care about retrieving/closing PersistenceManagers, |
||||
* or handling JDO lifecycle exceptions. |
||||
* |
||||
* <p>Typically used to implement data access or business logic services that |
||||
* use JDO within their implementation but are JDO-agnostic in their interface. |
||||
* The latter or code calling the latter only have to deal with business |
||||
* objects, query objects, and {@code org.springframework.dao} exceptions. |
||||
* |
||||
* <p>Can be used within a service implementation via direct instantiation |
||||
* with a PersistenceManagerFactory reference, or get prepared in an |
||||
* application context and given to services as bean reference. |
||||
* Note: The PersistenceManagerFactory should always be configured as bean in |
||||
* the application context, in the first case given to the service directly, |
||||
* in the second case to the prepared template. |
||||
* |
||||
* <p>This class can be considered as direct alternative to working with the |
||||
* raw JDO PersistenceManager API (through |
||||
* {@code PersistenceManagerFactoryUtils.getPersistenceManager()}). |
||||
* The major advantage is its automatic conversion to DataAccessExceptions, the |
||||
* major disadvantage that no checked application exceptions can get thrown from |
||||
* within data access code. Corresponding checks and the actual throwing of such |
||||
* exceptions can often be deferred to after callback execution, though. |
||||
* |
||||
* <p>{@link LocalPersistenceManagerFactoryBean} is the preferred way of obtaining |
||||
* a reference to a specific PersistenceManagerFactory, at least in a non-EJB |
||||
* environment. The Spring application context will manage its lifecycle, |
||||
* initializing and shutting down the factory as part of the application. |
||||
* |
||||
* <p>Note that lazy loading will just work with an open JDO PersistenceManager, |
||||
* either within a Spring-driven transaction (with JdoTransactionManager or |
||||
* JtaTransactionManager) or within OpenPersistenceManagerInViewFilter/Interceptor. |
||||
* Furthermore, some operations just make sense within transactions, |
||||
* for example: {@code evict}, {@code evictAll}, {@code flush}. |
||||
* |
||||
* <p><b>NOTE: This class requires JDO 2.0 or higher, as of Spring 2.5.</b> |
||||
* As of Spring 3.0, it follows JDO 2.1 conventions in terms of generic |
||||
* parameter and return types, which still remaining compatible with JDO 2.0. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 03.06.2003 |
||||
* @see #setPersistenceManagerFactory |
||||
* @see JdoCallback |
||||
* @see javax.jdo.PersistenceManager |
||||
* @see LocalPersistenceManagerFactoryBean |
||||
* @see JdoTransactionManager |
||||
* @see org.springframework.transaction.jta.JtaTransactionManager |
||||
* @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter |
||||
* @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor |
||||
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage |
||||
* (see {@link TransactionAwarePersistenceManagerFactoryProxy} and |
||||
* {@link org.springframework.orm.jdo.support.SpringPersistenceManagerProxyBean}) |
||||
*/ |
||||
@Deprecated |
||||
public class JdoTemplate extends JdoAccessor implements JdoOperations { |
||||
|
||||
private boolean allowCreate = true; |
||||
|
||||
private boolean exposeNativePersistenceManager = false; |
||||
|
||||
|
||||
/** |
||||
* Create a new JdoTemplate instance. |
||||
*/ |
||||
public JdoTemplate() { |
||||
} |
||||
|
||||
/** |
||||
* Create a new JdoTemplate instance. |
||||
* @param pmf PersistenceManagerFactory to create PersistenceManagers |
||||
*/ |
||||
public JdoTemplate(PersistenceManagerFactory pmf) { |
||||
setPersistenceManagerFactory(pmf); |
||||
afterPropertiesSet(); |
||||
} |
||||
|
||||
/** |
||||
* Create a new JdoTemplate instance. |
||||
* @param pmf PersistenceManagerFactory to create PersistenceManagers |
||||
* @param allowCreate if a non-transactional PersistenceManager should be created |
||||
* when no transactional PersistenceManager can be found for the current thread |
||||
*/ |
||||
public JdoTemplate(PersistenceManagerFactory pmf, boolean allowCreate) { |
||||
setPersistenceManagerFactory(pmf); |
||||
setAllowCreate(allowCreate); |
||||
afterPropertiesSet(); |
||||
} |
||||
|
||||
/** |
||||
* Set if a new PersistenceManager should be created when no transactional |
||||
* PersistenceManager can be found for the current thread. |
||||
* <p>JdoTemplate is aware of a corresponding PersistenceManager bound to the |
||||
* current thread, for example when using JdoTransactionManager. |
||||
* If allowCreate is true, a new non-transactional PersistenceManager will be |
||||
* created if none found, which needs to be closed at the end of the operation. |
||||
* If false, an IllegalStateException will get thrown in this case. |
||||
* @see PersistenceManagerFactoryUtils#getPersistenceManager(javax.jdo.PersistenceManagerFactory, boolean) |
||||
*/ |
||||
public void setAllowCreate(boolean allowCreate) { |
||||
this.allowCreate = allowCreate; |
||||
} |
||||
|
||||
/** |
||||
* Return if a new PersistenceManager should be created if no thread-bound found. |
||||
*/ |
||||
public boolean isAllowCreate() { |
||||
return this.allowCreate; |
||||
} |
||||
|
||||
/** |
||||
* Set whether to expose the native JDO PersistenceManager to JdoCallback |
||||
* code. Default is "false": a PersistenceManager proxy will be returned, |
||||
* suppressing {@code close} calls and automatically applying transaction |
||||
* timeouts (if any). |
||||
* <p>As there is often a need to cast to a provider-specific PersistenceManager |
||||
* class in DAOs that use provider-specific functionality, the exposed proxy |
||||
* implements all interfaces implemented by the original PersistenceManager. |
||||
* If this is not sufficient, turn this flag to "true". |
||||
* @see JdoCallback |
||||
* @see javax.jdo.PersistenceManager |
||||
* @see #prepareQuery |
||||
*/ |
||||
public void setExposeNativePersistenceManager(boolean exposeNativePersistenceManager) { |
||||
this.exposeNativePersistenceManager = exposeNativePersistenceManager; |
||||
} |
||||
|
||||
/** |
||||
* Return whether to expose the native JDO PersistenceManager to JdoCallback |
||||
* code, or rather a PersistenceManager proxy. |
||||
*/ |
||||
public boolean isExposeNativePersistenceManager() { |
||||
return this.exposeNativePersistenceManager; |
||||
} |
||||
|
||||
|
||||
public <T> T execute(JdoCallback<T> action) throws DataAccessException { |
||||
return execute(action, isExposeNativePersistenceManager()); |
||||
} |
||||
|
||||
public Collection executeFind(JdoCallback<?> action) throws DataAccessException { |
||||
Object result = execute(action, isExposeNativePersistenceManager()); |
||||
if (result != null && !(result instanceof Collection)) { |
||||
throw new InvalidDataAccessApiUsageException( |
||||
"Result object returned from JdoCallback isn't a Collection: [" + result + "]"); |
||||
} |
||||
return (Collection) result; |
||||
} |
||||
|
||||
/** |
||||
* Execute the action specified by the given action object within a |
||||
* PersistenceManager. |
||||
* @param action callback object that specifies the JDO action |
||||
* @param exposeNativePersistenceManager whether to expose the native |
||||
* JDO persistence manager to callback code |
||||
* @return a result object returned by the action, or {@code null} |
||||
* @throws org.springframework.dao.DataAccessException in case of JDO errors |
||||
*/ |
||||
public <T> T execute(JdoCallback<T> action, boolean exposeNativePersistenceManager) throws DataAccessException { |
||||
Assert.notNull(action, "Callback object must not be null"); |
||||
|
||||
PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager( |
||||
getPersistenceManagerFactory(), isAllowCreate()); |
||||
boolean existingTransaction = |
||||
TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory()); |
||||
try { |
||||
PersistenceManager pmToExpose = (exposeNativePersistenceManager ? pm : createPersistenceManagerProxy(pm)); |
||||
T result = action.doInJdo(pmToExpose); |
||||
flushIfNecessary(pm, existingTransaction); |
||||
return postProcessResult(result, pm, existingTransaction); |
||||
} |
||||
catch (JDOException ex) { |
||||
throw convertJdoAccessException(ex); |
||||
} |
||||
catch (RuntimeException ex) { |
||||
// callback code threw application exception
|
||||
throw ex; |
||||
} |
||||
finally { |
||||
PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Create a close-suppressing proxy for the given JDO PersistenceManager. |
||||
* Called by the {@code execute} method. |
||||
* <p>The proxy also prepares returned JDO Query objects. |
||||
* @param pm the JDO PersistenceManager to create a proxy for |
||||
* @return the PersistenceManager proxy, implementing all interfaces |
||||
* implemented by the passed-in PersistenceManager object (that is, |
||||
* also implementing all provider-specific extension interfaces) |
||||
* @see javax.jdo.PersistenceManager#close() |
||||
* @see #execute(JdoCallback, boolean) |
||||
* @see #prepareQuery |
||||
*/ |
||||
protected PersistenceManager createPersistenceManagerProxy(PersistenceManager pm) { |
||||
Class[] ifcs = ClassUtils.getAllInterfacesForClass(pm.getClass(), getClass().getClassLoader()); |
||||
return (PersistenceManager) Proxy.newProxyInstance( |
||||
pm.getClass().getClassLoader(), ifcs, new CloseSuppressingInvocationHandler(pm)); |
||||
} |
||||
|
||||
/** |
||||
* Post-process the given result object, which might be a Collection. |
||||
* Called by the {@code execute} method. |
||||
* <p>Default implementation always returns the passed-in Object as-is. |
||||
* Subclasses might override this to automatically detach result |
||||
* collections or even single result objects. |
||||
* @param pm the current JDO PersistenceManager |
||||
* @param result the result object (might be a Collection) |
||||
* @param existingTransaction if executing within an existing transaction |
||||
* (within an existing JDO PersistenceManager that won't be closed immediately) |
||||
* @return the post-processed result object (can be simply be the passed-in object) |
||||
* @see #execute(JdoCallback, boolean) |
||||
*/ |
||||
protected <T> T postProcessResult(T result, PersistenceManager pm, boolean existingTransaction) { |
||||
return result; |
||||
} |
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience methods for load, save, delete
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
public Object getObjectById(final Object objectId) throws DataAccessException { |
||||
return execute(new JdoCallback<Object>() { |
||||
public Object doInJdo(PersistenceManager pm) throws JDOException { |
||||
return pm.getObjectById(objectId, true); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public <T> T getObjectById(final Class<T> entityClass, final Object idValue) throws DataAccessException { |
||||
return execute(new JdoCallback<T>() { |
||||
public T doInJdo(PersistenceManager pm) throws JDOException { |
||||
return pm.getObjectById(entityClass, idValue); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public void evict(final Object entity) throws DataAccessException { |
||||
execute(new JdoCallback<Object>() { |
||||
public Object doInJdo(PersistenceManager pm) throws JDOException { |
||||
pm.evict(entity); |
||||
return null; |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public void evictAll(final Collection entities) throws DataAccessException { |
||||
execute(new JdoCallback<Object>() { |
||||
public Object doInJdo(PersistenceManager pm) throws JDOException { |
||||
pm.evictAll(entities); |
||||
return null; |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public void evictAll() throws DataAccessException { |
||||
execute(new JdoCallback<Object>() { |
||||
public Object doInJdo(PersistenceManager pm) throws JDOException { |
||||
pm.evictAll(); |
||||
return null; |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public void refresh(final Object entity) throws DataAccessException { |
||||
execute(new JdoCallback<Object>() { |
||||
public Object doInJdo(PersistenceManager pm) throws JDOException { |
||||
pm.refresh(entity); |
||||
return null; |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public void refreshAll(final Collection entities) throws DataAccessException { |
||||
execute(new JdoCallback<Object>() { |
||||
public Object doInJdo(PersistenceManager pm) throws JDOException { |
||||
pm.refreshAll(entities); |
||||
return null; |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public void refreshAll() throws DataAccessException { |
||||
execute(new JdoCallback<Object>() { |
||||
public Object doInJdo(PersistenceManager pm) throws JDOException { |
||||
pm.refreshAll(); |
||||
return null; |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public <T> T makePersistent(final T entity) throws DataAccessException { |
||||
return execute(new JdoCallback<T>() { |
||||
public T doInJdo(PersistenceManager pm) throws JDOException { |
||||
return pm.makePersistent(entity); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public <T> Collection<T> makePersistentAll(final Collection<T> entities) throws DataAccessException { |
||||
return execute(new JdoCallback<Collection<T>>() { |
||||
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException { |
||||
return pm.makePersistentAll(entities); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public void deletePersistent(final Object entity) throws DataAccessException { |
||||
execute(new JdoCallback<Object>() { |
||||
public Object doInJdo(PersistenceManager pm) throws JDOException { |
||||
pm.deletePersistent(entity); |
||||
return null; |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public void deletePersistentAll(final Collection entities) throws DataAccessException { |
||||
execute(new JdoCallback<Object>() { |
||||
public Object doInJdo(PersistenceManager pm) throws JDOException { |
||||
pm.deletePersistentAll(entities); |
||||
return null; |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public <T> T detachCopy(final T entity) { |
||||
return execute(new JdoCallback<T>() { |
||||
public T doInJdo(PersistenceManager pm) throws JDOException { |
||||
return pm.detachCopy(entity); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public <T> Collection<T> detachCopyAll(final Collection<T> entities) { |
||||
return execute(new JdoCallback<Collection<T>>() { |
||||
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException { |
||||
return pm.detachCopyAll(entities); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public void flush() throws DataAccessException { |
||||
execute(new JdoCallback<Object>() { |
||||
public Object doInJdo(PersistenceManager pm) throws JDOException { |
||||
pm.flush(); |
||||
return null; |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience finder methods
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
public <T> Collection<T> find(Class<T> entityClass) throws DataAccessException { |
||||
return find(entityClass, null, null); |
||||
} |
||||
|
||||
public <T> Collection<T> find(Class<T> entityClass, String filter) throws DataAccessException { |
||||
return find(entityClass, filter, null); |
||||
} |
||||
|
||||
public <T> Collection<T> find(final Class<T> entityClass, final String filter, final String ordering) |
||||
throws DataAccessException { |
||||
|
||||
return execute(new JdoCallback<Collection<T>>() { |
||||
@SuppressWarnings("unchecked") |
||||
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException { |
||||
Query query = (filter != null ? pm.newQuery(entityClass, filter) : pm.newQuery(entityClass)); |
||||
prepareQuery(query); |
||||
if (ordering != null) { |
||||
query.setOrdering(ordering); |
||||
} |
||||
return (Collection<T>) query.execute(); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public <T> Collection<T> find(Class<T> entityClass, String filter, String parameters, Object... values) |
||||
throws DataAccessException { |
||||
|
||||
return find(entityClass, filter, parameters, values, null); |
||||
} |
||||
|
||||
public <T> Collection<T> find( |
||||
final Class<T> entityClass, final String filter, final String parameters, final Object[] values, |
||||
final String ordering) throws DataAccessException { |
||||
|
||||
return execute(new JdoCallback<Collection<T>>() { |
||||
@SuppressWarnings("unchecked") |
||||
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException { |
||||
Query query = pm.newQuery(entityClass, filter); |
||||
prepareQuery(query); |
||||
query.declareParameters(parameters); |
||||
if (ordering != null) { |
||||
query.setOrdering(ordering); |
||||
} |
||||
return (Collection<T>) query.executeWithArray(values); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public <T> Collection<T> find( |
||||
Class<T> entityClass, String filter, String parameters, Map<String, ?> values) |
||||
throws DataAccessException { |
||||
|
||||
return find(entityClass, filter, parameters, values, null); |
||||
} |
||||
|
||||
public <T> Collection<T> find( |
||||
final Class<T> entityClass, final String filter, final String parameters, |
||||
final Map<String, ?> values, final String ordering) throws DataAccessException { |
||||
|
||||
return execute(new JdoCallback<Collection<T>>() { |
||||
@SuppressWarnings("unchecked") |
||||
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException { |
||||
Query query = pm.newQuery(entityClass, filter); |
||||
prepareQuery(query); |
||||
query.declareParameters(parameters); |
||||
if (ordering != null) { |
||||
query.setOrdering(ordering); |
||||
} |
||||
return (Collection<T>) query.executeWithMap(values); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public Collection find(final String language, final Object queryObject) throws DataAccessException { |
||||
return execute(new JdoCallback<Collection>() { |
||||
public Collection doInJdo(PersistenceManager pm) throws JDOException { |
||||
Query query = pm.newQuery(language, queryObject); |
||||
prepareQuery(query); |
||||
return (Collection) query.execute(); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public Collection find(final String queryString) throws DataAccessException { |
||||
return execute(new JdoCallback<Collection>() { |
||||
public Collection doInJdo(PersistenceManager pm) throws JDOException { |
||||
Query query = pm.newQuery(queryString); |
||||
prepareQuery(query); |
||||
return (Collection) query.execute(); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public Collection find(final String queryString, final Object... values) throws DataAccessException { |
||||
return execute(new JdoCallback<Collection>() { |
||||
public Collection doInJdo(PersistenceManager pm) throws JDOException { |
||||
Query query = pm.newQuery(queryString); |
||||
prepareQuery(query); |
||||
return (Collection) query.executeWithArray(values); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public Collection find(final String queryString, final Map<String, ?> values) throws DataAccessException { |
||||
return execute(new JdoCallback<Collection>() { |
||||
public Collection doInJdo(PersistenceManager pm) throws JDOException { |
||||
Query query = pm.newQuery(queryString); |
||||
prepareQuery(query); |
||||
return (Collection) query.executeWithMap(values); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public <T> Collection<T> findByNamedQuery(final Class<T> entityClass, final String queryName) |
||||
throws DataAccessException { |
||||
|
||||
return execute(new JdoCallback<Collection<T>>() { |
||||
@SuppressWarnings("unchecked") |
||||
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException { |
||||
Query query = pm.newNamedQuery(entityClass, queryName); |
||||
prepareQuery(query); |
||||
return (Collection<T>) query.execute(); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public <T> Collection<T> findByNamedQuery(final Class<T> entityClass, final String queryName, final Object... values) |
||||
throws DataAccessException { |
||||
|
||||
return execute(new JdoCallback<Collection<T>>() { |
||||
@SuppressWarnings("unchecked") |
||||
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException { |
||||
Query query = pm.newNamedQuery(entityClass, queryName); |
||||
prepareQuery(query); |
||||
return (Collection<T>) query.executeWithArray(values); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
public <T> Collection<T> findByNamedQuery( |
||||
final Class<T> entityClass, final String queryName, final Map<String, ?> values) |
||||
throws DataAccessException { |
||||
|
||||
return execute(new JdoCallback<Collection<T>>() { |
||||
@SuppressWarnings("unchecked") |
||||
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException { |
||||
Query query = pm.newNamedQuery(entityClass, queryName); |
||||
prepareQuery(query); |
||||
return (Collection<T>) query.executeWithMap(values); |
||||
} |
||||
}, true); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Prepare the given JDO query object. To be used within a JdoCallback. |
||||
* <p>Applies a transaction timeout, if any. If you don't use such timeouts, |
||||
* the call is a no-op. |
||||
* <p>In general, prefer a proxied PersistenceManager instead, which will |
||||
* automatically apply the transaction timeout (through the use of a special |
||||
* PersistenceManager proxy). You need to set the "exposeNativePersistenceManager" |
||||
* property to "false" to activate this. Note that you won't be able to cast |
||||
* to a provider-specific JDO PersistenceManager class anymore then. |
||||
* @param query the JDO query object |
||||
* @throws JDOException if the query could not be properly prepared |
||||
* @see JdoCallback#doInJdo |
||||
* @see PersistenceManagerFactoryUtils#applyTransactionTimeout |
||||
* @see #setExposeNativePersistenceManager |
||||
*/ |
||||
public void prepareQuery(Query query) throws JDOException { |
||||
PersistenceManagerFactoryUtils.applyTransactionTimeout( |
||||
query, getPersistenceManagerFactory(), getJdoDialect()); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Invocation handler that suppresses close calls on JDO PersistenceManagers. |
||||
* Also prepares returned Query objects. |
||||
* @see javax.jdo.PersistenceManager#close() |
||||
*/ |
||||
private class CloseSuppressingInvocationHandler implements InvocationHandler { |
||||
|
||||
private final PersistenceManager target; |
||||
|
||||
public CloseSuppressingInvocationHandler(PersistenceManager target) { |
||||
this.target = target; |
||||
} |
||||
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
||||
// Invocation on PersistenceManager interface (or provider-specific extension) coming in...
|
||||
|
||||
if (method.getName().equals("equals")) { |
||||
// Only consider equal when proxies are identical.
|
||||
return (proxy == args[0]); |
||||
} |
||||
else if (method.getName().equals("hashCode")) { |
||||
// Use hashCode of PersistenceManager proxy.
|
||||
return System.identityHashCode(proxy); |
||||
} |
||||
else if (method.getName().equals("close")) { |
||||
// Handle close method: suppress, not valid.
|
||||
return null; |
||||
} |
||||
|
||||
// Invoke method on target PersistenceManager.
|
||||
try { |
||||
Object retVal = method.invoke(this.target, args); |
||||
// If return value is a JDO Query object, apply transaction timeout.
|
||||
if (retVal instanceof Query) { |
||||
prepareQuery(((Query) retVal)); |
||||
} |
||||
return retVal; |
||||
} |
||||
catch (InvocationTargetException ex) { |
||||
throw ex.getTargetException(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,175 +0,0 @@
@@ -1,175 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed 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.springframework.orm.jdo.support; |
||||
|
||||
import javax.jdo.JDOException; |
||||
import javax.jdo.PersistenceManager; |
||||
import javax.jdo.PersistenceManagerFactory; |
||||
|
||||
import org.springframework.dao.DataAccessException; |
||||
import org.springframework.dao.DataAccessResourceFailureException; |
||||
import org.springframework.dao.support.DaoSupport; |
||||
import org.springframework.orm.jdo.JdoTemplate; |
||||
import org.springframework.orm.jdo.PersistenceManagerFactoryUtils; |
||||
|
||||
/** |
||||
* Convenient super class for JDO data access objects. |
||||
* |
||||
* <p>Requires a PersistenceManagerFactory to be set, providing a JdoTemplate |
||||
* based on it to subclasses. Can alternatively be initialized directly with a |
||||
* JdoTemplate, to reuse the latter's settings such as the PersistenceManagerFactory, |
||||
* JdoDialect, flush mode, etc. |
||||
* |
||||
* <p>This base class is mainly intended for JdoTemplate usage but can also |
||||
* be used when working with PersistenceManagerFactoryUtils directly, for example |
||||
* in combination with JdoInterceptor-managed PersistenceManagers. Convenience |
||||
* {@code getPersistenceManager} and {@code releasePersistenceManager} |
||||
* methods are provided for that usage style. |
||||
* |
||||
* <p>This class will create its own JdoTemplate if only a PersistenceManagerFactory |
||||
* is passed in. The "allowCreate" flag on that JdoTemplate will be "true" by default. |
||||
* A custom JdoTemplate instance can be used through overriding {@code createJdoTemplate}. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 28.07.2003 |
||||
* @see #setPersistenceManagerFactory |
||||
* @see #setJdoTemplate |
||||
* @see #createJdoTemplate |
||||
* @see #getPersistenceManager |
||||
* @see #releasePersistenceManager |
||||
* @see org.springframework.orm.jdo.JdoTemplate |
||||
* @see org.springframework.orm.jdo.JdoInterceptor |
||||
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage |
||||
* (see {@link org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy} |
||||
* and {@link SpringPersistenceManagerProxyBean}) |
||||
*/ |
||||
@Deprecated |
||||
public abstract class JdoDaoSupport extends DaoSupport { |
||||
|
||||
private JdoTemplate jdoTemplate; |
||||
|
||||
|
||||
/** |
||||
* Set the JDO PersistenceManagerFactory to be used by this DAO. |
||||
* Will automatically create a JdoTemplate for the given PersistenceManagerFactory. |
||||
* @see #createJdoTemplate |
||||
* @see #setJdoTemplate |
||||
*/ |
||||
public final void setPersistenceManagerFactory(PersistenceManagerFactory persistenceManagerFactory) { |
||||
if (this.jdoTemplate == null || persistenceManagerFactory != this.jdoTemplate.getPersistenceManagerFactory()) { |
||||
this.jdoTemplate = createJdoTemplate(persistenceManagerFactory); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Create a JdoTemplate for the given PersistenceManagerFactory. |
||||
* Only invoked if populating the DAO with a PersistenceManagerFactory reference! |
||||
* <p>Can be overridden in subclasses to provide a JdoTemplate instance |
||||
* with different configuration, or a custom JdoTemplate subclass. |
||||
* @param persistenceManagerFactory the JDO PersistenceManagerFactoryto create a JdoTemplate for |
||||
* @return the new JdoTemplate instance |
||||
* @see #setPersistenceManagerFactory |
||||
*/ |
||||
protected JdoTemplate createJdoTemplate(PersistenceManagerFactory persistenceManagerFactory) { |
||||
return new JdoTemplate(persistenceManagerFactory); |
||||
} |
||||
|
||||
/** |
||||
* Return the JDO PersistenceManagerFactory used by this DAO. |
||||
*/ |
||||
public final PersistenceManagerFactory getPersistenceManagerFactory() { |
||||
return (this.jdoTemplate != null ? this.jdoTemplate.getPersistenceManagerFactory() : null); |
||||
} |
||||
|
||||
/** |
||||
* Set the JdoTemplate for this DAO explicitly, |
||||
* as an alternative to specifying a PersistenceManagerFactory. |
||||
* @see #setPersistenceManagerFactory |
||||
*/ |
||||
public final void setJdoTemplate(JdoTemplate jdoTemplate) { |
||||
this.jdoTemplate = jdoTemplate; |
||||
} |
||||
|
||||
/** |
||||
* Return the JdoTemplate for this DAO, pre-initialized |
||||
* with the PersistenceManagerFactory or set explicitly. |
||||
*/ |
||||
public final JdoTemplate getJdoTemplate() { |
||||
return jdoTemplate; |
||||
} |
||||
|
||||
@Override |
||||
protected final void checkDaoConfig() { |
||||
if (this.jdoTemplate == null) { |
||||
throw new IllegalArgumentException("persistenceManagerFactory or jdoTemplate is required"); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Get a JDO PersistenceManager, either from the current transaction or |
||||
* a new one. The latter is only allowed if the "allowCreate" setting |
||||
* of this bean's JdoTemplate is true. |
||||
* @return the JDO PersistenceManager |
||||
* @throws DataAccessResourceFailureException if the PersistenceManager couldn't be created |
||||
* @throws IllegalStateException if no thread-bound PersistenceManager found and allowCreate false |
||||
* @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager |
||||
*/ |
||||
protected final PersistenceManager getPersistenceManager() { |
||||
return getPersistenceManager(this.jdoTemplate.isAllowCreate()); |
||||
} |
||||
|
||||
/** |
||||
* Get a JDO PersistenceManager, either from the current transaction or |
||||
* a new one. The latter is only allowed if "allowCreate" is true. |
||||
* @param allowCreate if a non-transactional PersistenceManager should be created |
||||
* when no transactional PersistenceManager can be found for the current thread |
||||
* @return the JDO PersistenceManager |
||||
* @throws DataAccessResourceFailureException if the PersistenceManager couldn't be created |
||||
* @throws IllegalStateException if no thread-bound PersistenceManager found and allowCreate false |
||||
* @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager |
||||
*/ |
||||
protected final PersistenceManager getPersistenceManager(boolean allowCreate) |
||||
throws DataAccessResourceFailureException, IllegalStateException { |
||||
|
||||
return PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), allowCreate); |
||||
} |
||||
|
||||
/** |
||||
* Convert the given JDOException to an appropriate exception from the |
||||
* org.springframework.dao hierarchy. |
||||
* <p>Delegates to the convertJdoAccessException method of this DAO's JdoTemplate. |
||||
* @param ex JDOException that occured |
||||
* @return the corresponding DataAccessException instance |
||||
* @see #setJdoTemplate |
||||
* @see org.springframework.orm.jdo.JdoTemplate#convertJdoAccessException |
||||
*/ |
||||
protected final DataAccessException convertJdoAccessException(JDOException ex) { |
||||
return this.jdoTemplate.convertJdoAccessException(ex); |
||||
} |
||||
|
||||
/** |
||||
* Close the given JDO PersistenceManager, created via this DAO's |
||||
* PersistenceManagerFactory, if it isn't bound to the thread. |
||||
* @param pm PersistenceManager to close |
||||
* @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#releasePersistenceManager |
||||
*/ |
||||
protected final void releasePersistenceManager(PersistenceManager pm) { |
||||
PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory()); |
||||
} |
||||
|
||||
} |
||||
@ -1,150 +0,0 @@
@@ -1,150 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed 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.springframework.orm.jdo; |
||||
|
||||
import java.lang.reflect.AccessibleObject; |
||||
import java.lang.reflect.Method; |
||||
|
||||
import javax.jdo.PersistenceManager; |
||||
import javax.jdo.PersistenceManagerFactory; |
||||
|
||||
import org.aopalliance.intercept.Interceptor; |
||||
import org.aopalliance.intercept.Invocation; |
||||
import org.aopalliance.intercept.MethodInvocation; |
||||
import org.junit.Test; |
||||
import org.springframework.transaction.support.TransactionSynchronizationManager; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import static org.mockito.BDDMockito.*; |
||||
|
||||
/** |
||||
* @author Juergen Hoeller |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class JdoInterceptorTests { |
||||
|
||||
@Test |
||||
public void testInterceptor() { |
||||
PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class); |
||||
PersistenceManager pm = mock(PersistenceManager.class); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
|
||||
JdoInterceptor interceptor = new JdoInterceptor(); |
||||
interceptor.setPersistenceManagerFactory(pmf); |
||||
try { |
||||
interceptor.invoke(new TestInvocation(pmf)); |
||||
} |
||||
catch (Throwable t) { |
||||
fail("Should not have thrown Throwable: " + t.getMessage()); |
||||
} |
||||
|
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testInterceptorWithPrebound() { |
||||
PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class); |
||||
PersistenceManager pm = mock(PersistenceManager.class); |
||||
|
||||
TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm)); |
||||
JdoInterceptor interceptor = new JdoInterceptor(); |
||||
interceptor.setPersistenceManagerFactory(pmf); |
||||
try { |
||||
interceptor.invoke(new TestInvocation(pmf)); |
||||
} |
||||
catch (Throwable t) { |
||||
fail("Should not have thrown Throwable: " + t.getMessage()); |
||||
} |
||||
finally { |
||||
TransactionSynchronizationManager.unbindResource(pmf); |
||||
} |
||||
} |
||||
|
||||
|
||||
@SuppressWarnings("unused") |
||||
private static class TestInvocation implements MethodInvocation { |
||||
|
||||
private PersistenceManagerFactory persistenceManagerFactory; |
||||
|
||||
public TestInvocation(PersistenceManagerFactory persistenceManagerFactory) { |
||||
this.persistenceManagerFactory = persistenceManagerFactory; |
||||
} |
||||
|
||||
@Override |
||||
public Object proceed() throws Throwable { |
||||
if (!TransactionSynchronizationManager.hasResource(this.persistenceManagerFactory)) { |
||||
throw new IllegalStateException("PersistenceManager not bound"); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public Object[] getArguments() { |
||||
return null; |
||||
} |
||||
|
||||
public int getCurrentInterceptorIndex() { |
||||
return 0; |
||||
} |
||||
|
||||
public int getNumberOfInterceptors() { |
||||
return 0; |
||||
} |
||||
|
||||
public Interceptor getInterceptor(int i) { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public Method getMethod() { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public AccessibleObject getStaticPart() { |
||||
return getMethod(); |
||||
} |
||||
|
||||
public Object getArgument(int i) { |
||||
return null; |
||||
} |
||||
|
||||
public void setArgument(int i, Object handler) { |
||||
} |
||||
|
||||
public int getArgumentCount() { |
||||
return 0; |
||||
} |
||||
|
||||
@Override |
||||
public Object getThis() { |
||||
return null; |
||||
} |
||||
|
||||
public Object getProxy() { |
||||
return null; |
||||
} |
||||
|
||||
public Invocation cloneInstance() { |
||||
return null; |
||||
} |
||||
|
||||
public void release() { |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,534 +0,0 @@
@@ -1,534 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed 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.springframework.orm.jdo; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import javax.jdo.JDODataStoreException; |
||||
import javax.jdo.JDOException; |
||||
import javax.jdo.JDOFatalDataStoreException; |
||||
import javax.jdo.JDOFatalUserException; |
||||
import javax.jdo.JDOObjectNotFoundException; |
||||
import javax.jdo.JDOOptimisticVerificationException; |
||||
import javax.jdo.JDOUserException; |
||||
import javax.jdo.PersistenceManager; |
||||
import javax.jdo.PersistenceManagerFactory; |
||||
import javax.jdo.Query; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.springframework.dao.DataIntegrityViolationException; |
||||
import org.springframework.transaction.support.TransactionSynchronizationManager; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import static org.mockito.BDDMockito.*; |
||||
|
||||
/** |
||||
* @author Juergen Hoeller |
||||
* @author Phillip Webb |
||||
* @since 03.06.2003 |
||||
*/ |
||||
public class JdoTemplateTests { |
||||
|
||||
private PersistenceManagerFactory pmf; |
||||
private PersistenceManager pm; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
pmf = mock(PersistenceManagerFactory.class); |
||||
pm = mock(PersistenceManager.class); |
||||
} |
||||
|
||||
@Test |
||||
public void testTemplateExecuteWithNotAllowCreate() { |
||||
JdoTemplate jt = new JdoTemplate(); |
||||
jt.setPersistenceManagerFactory(pmf); |
||||
jt.setAllowCreate(false); |
||||
try { |
||||
jt.execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
return null; |
||||
} |
||||
}); |
||||
fail("Should have thrown IllegalStateException"); |
||||
} |
||||
catch (IllegalStateException ex) { |
||||
// expected
|
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void testTemplateExecuteWithNotAllowCreateAndThreadBound() { |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.setAllowCreate(false); |
||||
TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm)); |
||||
final List l = new ArrayList(); |
||||
l.add("test"); |
||||
List result = (List) jt.execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
return l; |
||||
} |
||||
}); |
||||
assertTrue("Correct result list", result == l); |
||||
TransactionSynchronizationManager.unbindResource(pmf); |
||||
} |
||||
|
||||
@Test |
||||
public void testTemplateExecuteWithNewPersistenceManager() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
|
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
final List l = new ArrayList(); |
||||
l.add("test"); |
||||
List result = (List) jt.execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
return l; |
||||
} |
||||
}); |
||||
assertTrue("Correct result list", result == l); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testTemplateExecuteWithThreadBoundAndFlushEager() { |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.setFlushEager(true); |
||||
jt.setAllowCreate(false); |
||||
TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm)); |
||||
final List l = new ArrayList(); |
||||
l.add("test"); |
||||
List result = (List) jt.execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
return l; |
||||
} |
||||
}); |
||||
assertTrue("Correct result list", result == l); |
||||
TransactionSynchronizationManager.unbindResource(pmf); |
||||
verify(pm).flush(); |
||||
} |
||||
|
||||
@Test |
||||
public void testGetObjectById() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.getObjectById("0", true)).willReturn("A"); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals("A", jt.getObjectById("0")); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testGetObjectByIdWithClassAndValue() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.getObjectById(String.class, "0")).willReturn("A"); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals("A", jt.getObjectById(String.class, "0")); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testEvict() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.evict("0"); |
||||
verify(pm).evict("0"); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testEvictAllWithCollection() { |
||||
Collection coll = new HashSet(); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.evictAll(coll); |
||||
verify(pm).evictAll(coll); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testEvictAll() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.evictAll(); |
||||
verify(pm).evictAll(); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testRefresh() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.refresh("0"); |
||||
verify(pm).refresh("0"); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testRefreshAllWithCollection() { |
||||
Collection coll = new HashSet(); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.refreshAll(coll); |
||||
verify(pm).refreshAll(coll); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testRefreshAll() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.refreshAll(); |
||||
verify(pm).refreshAll(); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testMakePersistent() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.makePersistent("0"); |
||||
verify(pm).makePersistent("0"); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testMakePersistentAll() { |
||||
Collection coll = new HashSet(); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.makePersistentAll(coll); |
||||
verify(pm).makePersistentAll(coll); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testDeletePersistent() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.deletePersistent("0"); |
||||
verify(pm).deletePersistent("0"); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testDeletePersistentAll() { |
||||
Collection coll = new HashSet(); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.deletePersistentAll(coll); |
||||
verify(pm).deletePersistentAll(coll); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testDetachCopy() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.detachCopy("0")).willReturn("0x"); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals("0x", jt.detachCopy("0")); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testDetachCopyAll() { |
||||
Collection attached = new HashSet(); |
||||
Collection detached = new HashSet(); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.detachCopyAll(attached)).willReturn(detached); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals(detached, jt.detachCopyAll(attached)); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFlush() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.flush(); |
||||
verify(pm).flush(); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFlushWithDialect() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
jt.flush(); |
||||
verify(pm).flush(); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFind() { |
||||
Query query = mock(Query.class); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.newQuery(String.class)).willReturn(query); |
||||
Collection coll = new HashSet(); |
||||
given(query.execute()).willReturn(coll); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals(coll, jt.find(String.class)); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFindWithFilter() { |
||||
Query query = mock(Query.class); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.newQuery(String.class, "a == b")).willReturn(query); |
||||
Collection coll = new HashSet(); |
||||
given(query.execute()).willReturn(coll); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals(coll, jt.find(String.class, "a == b")); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFindWithFilterAndOrdering() { |
||||
Query query = mock(Query.class); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.newQuery(String.class, "a == b")).willReturn(query); |
||||
Collection coll = new HashSet(); |
||||
given(query.execute()).willReturn(coll); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals(coll, jt.find(String.class, "a == b", "c asc")); |
||||
verify(query).setOrdering("c asc"); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFindWithParameterArray() { |
||||
Query query = mock(Query.class); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.newQuery(String.class, "a == b")).willReturn(query); |
||||
Object[] values = new Object[0]; |
||||
Collection coll = new HashSet(); |
||||
given(query.executeWithArray(values)).willReturn(coll); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals(coll, jt.find(String.class, "a == b", "params", values)); |
||||
verify(query).declareParameters("params"); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFindWithParameterArrayAndOrdering() { |
||||
Query query = mock(Query.class); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.newQuery(String.class, "a == b")).willReturn(query); |
||||
Object[] values = new Object[0]; |
||||
Collection coll = new HashSet(); |
||||
given(query.executeWithArray(values)).willReturn(coll); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals(coll, jt.find(String.class, "a == b", "params", values, "c asc")); |
||||
verify(query).declareParameters("params"); |
||||
verify(query).setOrdering("c asc"); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFindWithParameterMap() { |
||||
Query query = mock(Query.class); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.newQuery(String.class, "a == b")).willReturn(query); |
||||
Map values = new HashMap(); |
||||
Collection coll = new HashSet(); |
||||
given(query.executeWithMap(values)).willReturn(coll); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals(coll, jt.find(String.class, "a == b", "params", values)); |
||||
verify(query).declareParameters("params"); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFindWithParameterMapAndOrdering() { |
||||
Query query = mock(Query.class); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.newQuery(String.class, "a == b")).willReturn(query); |
||||
Map values = new HashMap(); |
||||
Collection coll = new HashSet(); |
||||
given(query.executeWithMap(values)).willReturn(coll); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals(coll, jt.find(String.class, "a == b", "params", values, "c asc")); |
||||
verify(query).declareParameters("params"); |
||||
verify(query).setOrdering("c asc"); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFindWithLanguageAndQueryObject() { |
||||
Query query = mock(Query.class); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.newQuery(Query.SQL, "some SQL")).willReturn(query); |
||||
Collection coll = new HashSet(); |
||||
given(query.execute()).willReturn(coll); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals(coll, jt.find(Query.SQL, "some SQL")); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFindWithQueryString() { |
||||
Query query = mock(Query.class); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.newQuery("single string query")).willReturn(query); |
||||
Collection coll = new HashSet(); |
||||
given(query.execute()).willReturn(coll); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals(coll, jt.find("single string query")); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testFindByNamedQuery() { |
||||
Query query = mock(Query.class); |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
given(pm.newNamedQuery(String.class, "some query name")).willReturn(query); |
||||
Collection coll = new HashSet(); |
||||
given(query.execute()).willReturn(coll); |
||||
JdoTemplate jt = new JdoTemplate(pmf); |
||||
assertEquals(coll, jt.findByNamedQuery(String.class, "some query name")); |
||||
verify(pm).close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testTemplateExceptions() { |
||||
try { |
||||
createTemplate().execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
throw new JDOObjectNotFoundException(); |
||||
} |
||||
}); |
||||
fail("Should have thrown JdoObjectRetrievalFailureException"); |
||||
} |
||||
catch (JdoObjectRetrievalFailureException ex) { |
||||
// expected
|
||||
} |
||||
|
||||
try { |
||||
createTemplate().execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
throw new JDOOptimisticVerificationException(); |
||||
} |
||||
}); |
||||
fail("Should have thrown JdoOptimisticLockingFailureException"); |
||||
} |
||||
catch (JdoOptimisticLockingFailureException ex) { |
||||
// expected
|
||||
} |
||||
|
||||
try { |
||||
createTemplate().execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
throw new JDODataStoreException(); |
||||
} |
||||
}); |
||||
fail("Should have thrown JdoResourceFailureException"); |
||||
} |
||||
catch (JdoResourceFailureException ex) { |
||||
// expected
|
||||
} |
||||
|
||||
try { |
||||
createTemplate().execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
throw new JDOFatalDataStoreException(); |
||||
} |
||||
}); |
||||
fail("Should have thrown JdoResourceFailureException"); |
||||
} |
||||
catch (JdoResourceFailureException ex) { |
||||
// expected
|
||||
} |
||||
|
||||
try { |
||||
createTemplate().execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
throw new JDOUserException(); |
||||
} |
||||
}); |
||||
fail("Should have thrown JdoUsageException"); |
||||
} |
||||
catch (JdoUsageException ex) { |
||||
// expected
|
||||
} |
||||
|
||||
try { |
||||
createTemplate().execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
throw new JDOFatalUserException(); |
||||
} |
||||
}); |
||||
fail("Should have thrown JdoUsageException"); |
||||
} |
||||
catch (JdoUsageException ex) { |
||||
// expected
|
||||
} |
||||
|
||||
try { |
||||
createTemplate().execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
throw new JDOException(); |
||||
} |
||||
}); |
||||
fail("Should have thrown JdoSystemException"); |
||||
} |
||||
catch (JdoSystemException ex) { |
||||
// expected
|
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void testTranslateException() { |
||||
JdoDialect dialect = mock(JdoDialect.class); |
||||
final JDOException ex = new JDOException(); |
||||
given(dialect.translateException(ex)).willReturn(new DataIntegrityViolationException("test", ex)); |
||||
try { |
||||
JdoTemplate template = createTemplate(); |
||||
template.setJdoDialect(dialect); |
||||
template.execute(new JdoCallback() { |
||||
@Override |
||||
public Object doInJdo(PersistenceManager pm) { |
||||
throw ex; |
||||
} |
||||
}); |
||||
fail("Should have thrown DataIntegrityViolationException"); |
||||
} |
||||
catch (DataIntegrityViolationException dive) { |
||||
// expected
|
||||
} |
||||
} |
||||
|
||||
private JdoTemplate createTemplate() { |
||||
given(pmf.getPersistenceManager()).willReturn(pm); |
||||
return new JdoTemplate(pmf); |
||||
} |
||||
|
||||
} |
||||
@ -1,71 +0,0 @@
@@ -1,71 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed 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.springframework.orm.jdo.support; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import javax.jdo.PersistenceManagerFactory; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.orm.jdo.JdoTemplate; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import static org.mockito.BDDMockito.*; |
||||
|
||||
/** |
||||
* @author Juergen Hoeller |
||||
* @author Phillip Webb |
||||
* @since 30.07.2003 |
||||
*/ |
||||
public class JdoDaoSupportTests { |
||||
|
||||
@Test |
||||
public void testJdoDaoSupportWithPersistenceManagerFactory() throws Exception { |
||||
PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class); |
||||
pmf.getConnectionFactory(); |
||||
final List test = new ArrayList(); |
||||
JdoDaoSupport dao = new JdoDaoSupport() { |
||||
@Override |
||||
protected void initDao() { |
||||
test.add("test"); |
||||
} |
||||
}; |
||||
dao.setPersistenceManagerFactory(pmf); |
||||
dao.afterPropertiesSet(); |
||||
assertEquals("Correct PersistenceManagerFactory", pmf, dao.getPersistenceManagerFactory()); |
||||
assertEquals("Correct JdoTemplate", pmf, dao.getJdoTemplate().getPersistenceManagerFactory()); |
||||
assertEquals("initDao called", test.size(), 1); |
||||
} |
||||
|
||||
@Test |
||||
public void testJdoDaoSupportWithJdoTemplate() throws Exception { |
||||
JdoTemplate template = new JdoTemplate(); |
||||
final List test = new ArrayList(); |
||||
JdoDaoSupport dao = new JdoDaoSupport() { |
||||
@Override |
||||
protected void initDao() { |
||||
test.add("test"); |
||||
} |
||||
}; |
||||
dao.setJdoTemplate(template); |
||||
dao.afterPropertiesSet(); |
||||
assertEquals("Correct JdoTemplate", template, dao.getJdoTemplate()); |
||||
assertEquals("initDao called", test.size(), 1); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue