/** * Clear the persistence context, causing all managed entities to become detached. * Changes made to entities that have not been flushed to the database will not be persisted. */ public void clear() { assertIsOpen(); om.detachAll(); om.clearDirty(); om.evictAllObjects(); }
assertEntity(entityClass); AbstractClassMetaData acmd = om.getMetaDataManager().getMetaDataForClass(entityClass, om.getClassLoaderResolver()); if (acmd == null) pc = om.findObject(primaryKey, true, true, null); try id = om.newObjectId(entityClass, primaryKey); pc = om.findObject(id, true, true, null); if (om.getApiAdapter().isTransactional(pc)) StateManager sm = om.findStateManager(pc); if (om.getApiAdapter().isDeleted(pc))
/** * Convenience accessor for setting a transaction option. * @param option option name * @param value The value */ public void setOption(String option, int value) { om.getTransaction().setOption(option, value); }
/** * * Constructor for instantiating the Force.com object manager. * * @param apmf Persistence Manager Factory * @param userName Username for the datastore * @param password Password for the datastore */ public ForceJDOPersistenceManager(JDOPersistenceManagerFactory apmf, String userName, String password) { super(apmf, userName, password); this.objectMgr = new ForceObjectManagerImpl(apmf, this, userName, password); fetchPlan = new JDOFetchPlan(objectMgr.getFetchPlan()); setTransaction(objectMgr.getTransaction()); } }
/** * Method added to allow a StateManager to be injected into a transient object * so that we can track which fields have been touched. Then if the user calls * <code>merge()</code> on that transient object we can copy the fields over to a detached * object and call <code>merge()</code> on it. If the user called persist instead, we simply * discard this injected StateManager and proceed as usual. * * @param pc PersistenceCapable */ public void injectStateManagerIntoTransient(PersistenceCapable pc) { AbstractClassMetaData acmd = om.getMetaDataManager().getMetaDataForClass(pc.getClass(), om.getClassLoaderResolver()); ForceJPAStateManagerImpl sm = new ForceJPAStateManagerImpl(om, acmd); sm.initialiseForHollowPreConstructed(null, pc); om.putObjectIntoCache(sm); if (acmd.hasVersionStrategy()) { // This is not the right value but we need something to pacify DataNucleus. // We require that the user set a valid version before calling merge sm.setVersion(EPOCH_TIME); } }
/** * Determine if the Bean Validation provider is allowed to reach the property state * @param traversableObject object hosting <code>traversableProperty</code> or null if <code>validateValue</code> is * called * @param traversableProperty the traversable property. * @param rootBeanType type of the root object passed to the Validator. * @param pathToTraversableObject path from the root object to <code>traversableObject</code> (using the path * specification defined by Bean Validator). * @param elementType either <code>FIELD</code> or <code>METHOD</code>. * @return <code>true</code> if the Bean Validation provider is allowed to reach the property state, * <code>false</code> otherwise. */ public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) { AbstractClassMetaData acmd = om.getMetaDataManager().getMetaDataForClass(traversableObject.getClass(), om.getClassLoaderResolver()); if (acmd == null) { return false; } int fieldNumber = acmd.getMetaDataForMember(traversableProperty.getName()).getAbsoluteFieldNumber(); boolean loaded = om.getApiAdapter().isLoaded(om.findStateManager(traversableObject).getObjectProvider(), fieldNumber); if (loaded) { return true; } return false; } }
/** * Create an instance of Query for executing an SOQL query. * @param soqlString a native SOQL query string * @param resultSetMapping the name of the result set mapping * @return the new query instance */ @Override public Query createNativeQuery(String soqlString, String resultSetMapping) { assertIsOpen(); try { org.datanucleus.store.query.Query internalQuery = om.getOMFContext().getQueryManager().newQuery( SOQL_LANGUAGE, om.getExecutionContext(), soqlString); QueryResultMetaData qrmd = om.getMetaDataManager().getMetaDataForQueryResult(resultSetMapping); if (qrmd == null) { // TODO Localise this, and check if it is the correct exception to throw throw new IllegalArgumentException("ResultSetMapping " + resultSetMapping + " is not found"); } internalQuery.setResultMetaData(qrmd); return new JPAQuery(this, internalQuery, SOQL_LANGUAGE); } catch (NucleusException ne) { throw new IllegalArgumentException(ne.getMessage(), ne); } }
ClassLoaderResolver clr = om.getClassLoaderResolver(); QueryMetaData qmd = om.getMetaDataManager().getMetaDataForQuery(null, clr, queryName); if (qmd == null) org.datanucleus.store.query.Query internalQuery = om.getOMFContext().getQueryManager().newQuery( qmd.getLanguage().toString(), om.getExecutionContext(), qmd.getQuery()); return new JPAQuery(this, internalQuery, qmd.getLanguage()); org.datanucleus.store.query.Query internalQuery = om.getOMFContext().getQueryManager().newQuery( qmd.getLanguage(), om.getExecutionContext(), qmd.getQuery()); if (qmd.getResultClass() != null) try resultClass = om.getClassLoaderResolver().classForName(resultClassName); internalQuery.setResultClass(resultClass); return new JPAQuery(this, internalQuery, qmd.getLanguage()); QueryResultMetaData qrmd = om.getMetaDataManager().getMetaDataForQueryResult(qmd.getResultMetaDataName()); if (qrmd == null)
@Override public Object merge(Object entity) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("JPA Merge: - entity: " + entity); } if (entity instanceof PersistenceCapable) { PersistenceCapable pc = (PersistenceCapable) entity; if (pc.jdoIsDetached() && pc.jdoGetObjectId() == null) { throw new IllegalArgumentException("Detached entity with null id cannot be merged."); } // Read id from entity StateManager sm = om.findStateManager(entity); if (sm != null) { // This path is taken by all merge() calls. // We need to isolate merge() calls that were made to Transient object instead of Detached objects AbstractClassMetaData acmd = om.getMetaDataManager() .getMetaDataForClass(entity.getClass(), om.getClassLoaderResolver()); Object id = ForceQueryUtils.getIdFromObject(pc, acmd); if (id != null) { if (sm instanceof ForceJPAStateManagerImpl) { sm.initialiseForDetached(pc, id, sm.getVersion(pc)); entity = sm.getObject(); } } } } return super.merge(entity); }
final ClassLoaderResolver clr = om.getClassLoaderResolver(); AbstractClassMetaData acmd = om.getOMFContext().getMetaDataManager().getMetaDataForClass(pc.getClass(), clr); List<EventListenerMetaData> listenerMetaData = om.getMetaDataManager().getListeners(); if (listenerMetaData != null && listenerMetaData.size() > 0)
if (getBooleanProperty("datanucleus.replicateObjectGraph")) om.getFetchPlan().setGroup("all"); om.getFetchPlan().setMaxFetchDepth(-1); StoreManager storeMgr = om.getStoreManager(); ClassLoaderResolver clr = om.getClassLoaderResolver(); try String className = storeMgr.getClassNameForObjectID(oids[i], clr, om.getExecutionContext()); if (className == null)
public void refresh(Object entity, LockModeType lock, Map<String, Object> properties) { // TODO Use lock, properties assertLockModeValid(lock); assertIsOpen(); assertTransactionNotRequired(); assertEntity(entity); if (om.getApiAdapter().getPersistenceManager(entity) != pm) { throw new IllegalArgumentException(LOCALISER.msg("EM.EntityIsNotManaged", StringUtils.toJVMIDString(entity))); } if (!om.exists(entity)) { throwException(new EntityNotFoundException(LOCALISER.msg("EM.EntityNotInDatastore", StringUtils.toJVMIDString(entity)))); } try { if (lock != null && lock != LockModeType.NONE) { // Register the object for locking om.getLockManager().lock(om.getApiAdapter().getIdForObject(entity), getLockTypeForJPALockModeType(lock)); } om.refreshObject(entity); } catch (NucleusException ne) { throwException(NucleusJPAHelper.getJPAExceptionForNucleusException(ne)); } }
/** * Create an instance of Query for executing an SOQL query. * @param soqlString a native SOQL query string * @param resultClass the class of the resulting instance(s) * @return the new query instance */ @Override public Query createNativeQuery(String soqlString, Class resultClass) { assertIsOpen(); try { org.datanucleus.store.query.Query internalQuery = om.getOMFContext().getQueryManager().newQuery( SOQL_LANGUAGE, om.getExecutionContext(), soqlString); if (resultClass != null) { internalQuery.setResultClass(resultClass); } return new JPAQuery(this, internalQuery, SOQL_LANGUAGE); } catch (NucleusException ne) { throw new IllegalArgumentException(ne.getMessage(), ne); } }
assertTransactionNotRequired(); assertEntity(entity); if (om.exists(entity)) if (om.getApiAdapter().isDetached(entity)) om.persistObject(entity);
StateManager sm = om.findStateManager(entity); if (sm != null) { if (sm instanceof ForceJPAStateManagerImpl) { om.clearDirty(sm); om.removeStateManager(sm); pc.jdoReplaceStateManager(null); } else {
/** * Refresh the state of the instance from the database, overwriting changes made to the entity, if any. * @param entity The Entity * @throws IllegalArgumentException if not an entity or entity is not managed * @throws TransactionRequiredException if invoked on a container-managed entity manager * of type PersistenceContextType.TRANSACTION and there is no transaction. * @throws EntityNotFoundException if the entity no longer exists in the database */ public void refresh(Object entity) { assertIsOpen(); assertTransactionNotRequired(); assertEntity(entity); if (om.getApiAdapter().getPersistenceManager(entity) != pm) { throw new IllegalArgumentException(LOCALISER.msg("EM.EntityIsNotManaged", StringUtils.toJVMIDString(entity))); } if (!om.exists(entity)) { throw new EntityNotFoundException(LOCALISER.msg("EM.EntityNotInDatastore", StringUtils.toJVMIDString(entity))); } try { om.refreshObject(entity); } catch (NucleusException jpe) { throw NucleusJPAHelper.getJPAExceptionForNucleusException(jpe); } }
PersistenceConfiguration conf = om.getOMFContext().getPersistenceConfiguration(); if (conf.hasProperty("javax.persistence.validation.mode")) Class cls = om.getClassLoaderResolver().classForName("org.datanucleus.jpa.beanvalidation.BeanValidatorHandler"); Constructor constructor = cls.getConstructor(new Class[]{ObjectManager.class}); return (CallbackHandler) constructor.newInstance(new Object[]{om});
/** * Get the current lock mode for the entity instance. * @param entity The entity in question * @return lock mode * @throws TransactionRequiredException if there is no transaction * @throws IllegalArgumentException if the instance is not a managed entity and a transaction is active */ public LockModeType getLockMode(Object entity) { assertIsActive(); assertEntity(entity); if (om.getApiAdapter().getPersistenceManager(entity) != pm) { throw new IllegalArgumentException(LOCALISER.msg("EM.EntityIsNotManaged", StringUtils.toJVMIDString(entity))); } if (om.getApiAdapter().isDetached(entity)) { throw new IllegalArgumentException(LOCALISER.msg("EM.EntityIsNotManaged", StringUtils.toJVMIDString(entity))); } StateManager sm = om.findStateManager(entity); return getJPALockModeTypeForLockType(sm.getLockMode()); }
/** * Create an instance of Query for executing an SQL query. * @param sqlString a native SQL query string * @param resultSetMapping the name of the result set mapping * @return the new query instance */ public Query createNativeQuery(String sqlString, String resultSetMapping) { assertIsOpen(); try { org.datanucleus.store.query.Query jpoxQuery = getObjectManager().getOMFContext().getQueryManager().newQuery( QueryLanguage.SQL.toString(), getObjectManager(), sqlString); QueryResultMetaData qrmd = om.getMetaDataManager().getMetaDataForQueryResult(resultSetMapping); if (qrmd == null) { // TODO Localise this, and check if it is the correct exception to throw throw new IllegalArgumentException("ResultSetMapping " + resultSetMapping + " is not found"); } jpoxQuery.setResultMetaData(qrmd); return new JPAQuery(this, jpoxQuery, QueryLanguage.SQL.toString()); } catch (NucleusException jpe) { throw new IllegalArgumentException(jpe.getMessage(), jpe); } }
/** * Determine whether the EntityManager is open. * @return true until the EntityManager has been closed. */ public boolean isOpen() { return !om.isClosed(); }