📄 jdotemplate.java
字号:
}
//-------------------------------------------------------------------------
// Convenience methods for load, save, delete
//-------------------------------------------------------------------------
public Object getObjectById(final Serializable objectId) throws DataAccessException {
return execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
return pm.getObjectById(objectId, true);
}
});
}
public Object getObjectById(final Class entityClass, final Serializable idValue) throws DataAccessException {
return execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Object oid = pm.newObjectIdInstance(entityClass, idValue.toString());
return pm.getObjectById(oid, true);
}
});
}
public void evict(final Object entity) throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.evict(entity);
return null;
}
});
}
public void evictAll() throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.evictAll();
return null;
}
});
}
public void refresh(final Object entity) throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.refresh(entity);
return null;
}
});
}
public void refreshAll() throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.refreshAll();
return null;
}
});
}
public void makePersistent(final Object entity) throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.makePersistent(entity);
return null;
}
});
}
public void deletePersistent(final Object entity) throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.deletePersistent(entity);
return null;
}
});
}
public void deletePersistentAll(final Collection entities) throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.deletePersistentAll(entities);
return null;
}
});
}
public void flush() throws DataAccessException {
execute(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
getJdoDialect().flush(pm);
return null;
}
});
}
//-------------------------------------------------------------------------
// Convenience finder methods
//-------------------------------------------------------------------------
public Collection find(Class entityClass) throws DataAccessException {
return find(entityClass, null, null);
}
public Collection find(Class entityClass, String filter) throws DataAccessException {
return find(entityClass, filter, null);
}
public Collection find(final Class entityClass, final String filter, final String ordering)
throws DataAccessException {
return executeFind(new JdoCallback() {
public Object 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 query.execute();
}
});
}
public Collection find(Class entityClass, String filter, String parameters, Object[] values)
throws DataAccessException {
return find(entityClass, filter, parameters, values, null);
}
public Collection find(
final Class entityClass, final String filter, final String parameters, final Object[] values,
final String ordering) throws DataAccessException {
return executeFind(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(entityClass, filter);
prepareQuery(query);
query.declareParameters(parameters);
if (ordering != null) {
query.setOrdering(ordering);
}
return query.executeWithArray(values);
}
});
}
public Collection find(Class entityClass, String filter, String parameters, Map values)
throws DataAccessException {
return find(entityClass, filter, parameters, values, null);
}
public Collection find(
final Class entityClass, final String filter, final String parameters, final Map values,
final String ordering) throws DataAccessException {
return executeFind(new JdoCallback() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(entityClass, filter);
prepareQuery(query);
query.declareParameters(parameters);
if (ordering != null) {
query.setOrdering(ordering);
}
return query.executeWithMap(values);
}
});
}
/**
* Prepare the given JDO query object. To be used within a JdoCallback.
* 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 vendor-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 and Criteria objects.
* @see javax.jdo.PersistenceManager#close
*/
private class CloseSuppressingInvocationHandler implements InvocationHandler {
private static final String PERSISTENCE_MANAGER_CLOSE_METHOD_NAME = "close";
private final PersistenceManager target;
public CloseSuppressingInvocationHandler(PersistenceManager target) {
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Handle close method: suppress, not valid.
if (method.getName().equals(PERSISTENCE_MANAGER_CLOSE_METHOD_NAME)) {
return null;
}
// Invoke method on target connection.
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();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -