@Test public void testCanCastSharedEntityManagerProxyToEclipseLinkEntityManager() { assertTrue(sharedEntityManager instanceof JpaEntityManager); JpaEntityManager eclipselinkEntityManager = (JpaEntityManager) sharedEntityManager; assertNotNull(eclipselinkEntityManager.getActiveSession()); }
/** * Gets the client session. * * @param em the em * @return the client session */ public AbstractSession getClientSession(EntityManager em) { UnitOfWork uow = JpaHelper.getEntityManager(em).getUnitOfWork(); return (AbstractSession) uow; }
/** * Returns the current Compass session associated with the {@link javax.persistence.EntityManager}. * Compass Session is associated with an Entity Manager when a transaction is started and removed when the * transaction commits/rollsback. * * <p>The session can be used to perform searches that needs to take into account current transactional changes * or to perform additional Compass operations that are not reflected by the mirroring feature. */ public static CompassSession getCurrentCompassSession(EntityManager em) { JpaEntityManager nativeEm = (JpaEntityManager) NativeJpaHelper.extractNativeJpa(em); return findCompassSessionEventListener(nativeEm.getServerSession()).getCurrentCompassSession(nativeEm.getUnitOfWork()); }
public EntityInformation[] locate(EntityManagerFactory entityManagerFactory, JpaGpsDevice device) throws JpaGpsDeviceException { CompassGpsInterfaceDevice gps = (CompassGpsInterfaceDevice) device.getGps(); JpaEntityManager entityManager = (JpaEntityManager) entityManagerFactory.createEntityManager(); Session session = entityManager.getServerSession(); entityManager.close(); ArrayList<EntityInformation> entitiesList = new ArrayList<EntityInformation>(); Map descriptors = session.getDescriptors(); for (Object o : descriptors.values()) { ClassDescriptor classDescriptor = (ClassDescriptor) o; String entityname = classDescriptor.getJavaClassName(); if (!gps.hasMappingForEntityForIndex((entityname))) { if (log.isDebugEnabled()) { log.debug("Entity [" + entityname + "] does not have compass mapping, filtering it out"); } continue; } if (shouldFilter(entityname, classDescriptor, device)) { continue; } Class<?> clazz = classDescriptor.getJavaClass(); ResourceMapping resourceMapping = gps.getMappingForEntityForIndex(entityname); EntityInformation entityInformation = new EntityInformation(clazz, classDescriptor.getAlias(), resourceMapping.getSubIndexHash().getSubIndexes()); entitiesList.add(entityInformation); if (log.isDebugEnabled()) { log.debug("Entity [" + entityname + "] will be indexed"); } } return entitiesList.toArray(new EntityInformation[entitiesList.size()]); }
public void addListener(JREntityListener t) { JpaEntityManager entityManager = null; try { // Create an entity manager for use in this function entityManager = (JpaEntityManager) entityManagerFactory.createEntityManager(); // Use the entity manager to get a ClassDescriptor for the Entity class ClassDescriptor desc = entityManager.getSession().getClassDescriptor(t.getTarget()); JRDescriptorEventListener jrDescriptorEventListener = new JRDescriptorEventListener(t); desc.getEventManager().addListener(jrDescriptorEventListener); logger.info("Entity Listener for " + t.getTarget().getCanonicalName() + " is added"); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (entityManager != null) { // Cleanup the entity manager entityManager.close(); } } }
public JPADynamicHelper(EntityManager em) { super(JpaHelper.getEntityManager(em).getDatabaseSession()); }
private static CompassSessionEventListener findCompassSessionEventListener(EntityManager em) throws CompassException { return findCompassSessionEventListener(((JpaEntityManager) NativeJpaHelper.extractNativeJpa(em)).getServerSession()); }
/** * Given an EntityManager return the EntityManagerFactory that created it. This method must be called * on an open entity manager and will return null if called on a closed entityManager. * * This method will return null for non-EclipseLink EntityManagers. * * @see JpaEntityManagerFactory */ public static JpaEntityManagerFactory getEntityManagerFactory(javax.persistence.EntityManager em) { JpaEntityManager entityManager = getEntityManager(em); if (entityManager != null){ if (entityManager.getEntityManagerFactory() != null){ return ((EntityManagerFactoryDelegate)entityManager.getEntityManagerFactory()).getOwner(); } } return null; }
/** * Gets the client session. * * @param em the em * @return the client session */ public AbstractSession getClientSession(EntityManager em) { UnitOfWork uow = JpaHelper.getEntityManager(em).getUnitOfWork(); return (AbstractSession) uow; }
public void removeLifecycle(EntityManagerFactory entityManagerFactory, JpaGpsDevice device) throws JpaGpsDeviceException { CompassGpsInterfaceDevice gps = (CompassGpsInterfaceDevice) device.getGps(); JpaEntityManager entityManager = (JpaEntityManager) entityManagerFactory.createEntityManager(); Session session = entityManager.getServerSession(); entityManager.close(); Map descriptors = session.getDescriptors(); for (Object o : descriptors.values()) { ClassDescriptor classDescriptor = (ClassDescriptor) o; Class mappedClass = classDescriptor.getJavaClass(); // if we have a parent class that is mapped in JPA and in Compass, then don't add an event listner // since we will add it to the parent descriptor and it will notify this class as well if (classDescriptor.isChildDescriptor()) { Class parentClass = classDescriptor.getInheritancePolicy().getParentDescriptor().getJavaClass(); if (gps.hasMappingForEntityForMirror(parentClass, Cascade.ALL)) { classDescriptor.getDescriptorEventManager().removeListener(dummyEventListener); continue; } } if (gps.hasMappingForEntityForMirror(mappedClass, Cascade.ALL)) { classDescriptor.getDescriptorEventManager().removeListener(eventListener); } } } }
/** * Gets the descriptor for the entity type. * * @param entityClass the type of the enty. * @return the descriptor for the entity type. */ protected ClassDescriptor getClassDescriptor(Class<?> entityClass) { return getEclipseLinkEntityManager().getDatabaseSession().getDescriptor(entityClass); }
/** * @see org.seasar.framework.jpa.Dialect#getConnection(javax.persistence.EntityManager) */ public Connection getConnection(EntityManager em) { Object delegate = em.getDelegate(); JpaEntityManager eclipselinkEm = JpaEntityManager.class.cast(delegate); ServerSession session = eclipselinkEm.getServerSession(); return session.getLogin().getConnector().connect( new Properties(), session); }
@Override public int executeUpdate() { JpaQuery<T> jpaQuery = getQuery(); DatabaseQuery databaseQuery = jpaQuery.getDatabaseQuery(); Class referenceClass = databaseQuery.getReferenceClass(); boolean isDeleteQuery = databaseQuery.isDeleteObjectQuery() || databaseQuery.isDeleteAllQuery(); boolean enableDeleteInSoftDeleteMode = Boolean.parseBoolean(AppContext.getProperty("cuba.enableDeleteStatementInSoftDeleteMode")); if (!enableDeleteInSoftDeleteMode && entityManager.isSoftDeletion() && isDeleteQuery) { if (SoftDelete.class.isAssignableFrom(referenceClass)) { throw new UnsupportedOperationException("Delete queries are not supported with enabled soft deletion. " + "Use 'cuba.enableDeleteStatementInSoftDeleteMode' application property to roll back to legacy behavior."); } } // In some cache configurations (in particular, when shared cache is on, but for some entities cache is set to ISOLATED), // EclipseLink does not evict updated entities from cache automatically. Cache cache = jpaQuery.getEntityManager().getEntityManagerFactory().getCache(); if (referenceClass != null) { cache.evict(referenceClass); queryCacheMgr.invalidate(referenceClass, true); } else { cache.evictAll(); queryCacheMgr.invalidateAll(true); } preExecute(jpaQuery); return jpaQuery.executeUpdate(); }
/** * @see org.seasar.framework.jpa.Dialect#detach(javax.persistence.EntityManager, java.lang.Object) */ public void detach(EntityManager em, Object managedEntity) { Object delegate = em.getDelegate(); JpaEntityManager eclipselinkEm = JpaEntityManager.class.cast(delegate); UnitOfWork work = eclipselinkEm.getUnitOfWork(); work.unregisterObject(managedEntity); }
@Override public boolean containsEntity(EntityManager em, Class<?> entityClass, Object id) { return em.unwrap(JpaEntityManager.class).getActiveSession().getIdentityMapAccessor().getFromIdentityMap(id, entityClass) != null; }
public void injectLifecycle(EntityManagerFactory entityManagerFactory, JpaGpsDevice device) throws JpaGpsDeviceException { CompassGpsInterfaceDevice gps = (CompassGpsInterfaceDevice) device.getGps(); if (eventListener == null) { eventListener = new TopLinkEssentialsEventListener(device); } JpaEntityManager entityManager = (JpaEntityManager) entityManagerFactory.createEntityManager(); Session session = entityManager.getServerSession(); entityManager.close(); Map descriptors = session.getDescriptors(); for (Object o : descriptors.values()) { ClassDescriptor classDescriptor = (ClassDescriptor) o; Class mappedClass = classDescriptor.getJavaClass(); // if we have a parent class that is mapped in JPA and in Compass, then don't add an event listner // since we will add it to the parent descriptor and it will notify this class as well if (classDescriptor.isChildDescriptor()) { Class parentClass = classDescriptor.getInheritancePolicy().getParentDescriptor().getJavaClass(); if (gps.hasMappingForEntityForMirror(parentClass, Cascade.ALL)) { // we need to inject a dummy listener, just so it will kick in the listener notifications classDescriptor.getDescriptorEventManager().addListener(dummyEventListener); continue; } } if (gps.hasMappingForEntityForMirror(mappedClass, Cascade.ALL)) { classDescriptor.getDescriptorEventManager().addListener(eventListener); } } }
@SuppressWarnings("unchecked") @Override public T call() { T copiedObject = (T) sharedEntityManager.unwrap(JpaEntityManager.class).getDatabaseSession() .copy(dataObject, copyGroup); if (shouldResetObjectId) { clearObjectIdOnUpdatableObjects(copiedObject, new HashSet<Object>()); } if (shouldResetVersionNumber) { clearVersionNumberOnUpdatableObjects(copiedObject, new HashSet<Object>()); } return copiedObject; } });
Account a = em.merge(account); final JpaEntityManager jpaEntityManager = (JpaEntityManager) em.getDelegate(); final UnitOfWorkChangeSet changeSet = jpaEntityManager.getUnitOfWork().getCurrentChanges(); final ObjectChangeSet accountChangeSet = changeSet.getObjectChangeSetForClone(a); final ObjectChangeSet userChangeSet = changeSet.getObjectChangeSetForClone(a.getUser()); em.clear(); //checks only for these 2 specific fields; changeSet.hasChanges() would check for all changes return accountChangeSet.hasChangeFor("credits") || userChangeSet.hasChangeFor("name");
@Override public boolean containsEntity(EntityManager em, Class<?> entityClass, Object id) { return em.unwrap(JpaEntityManager.class).getActiveSession().getIdentityMapAccessor().getFromIdentityMap(id, entityClass) != null; }
dm.preInitialize(getEclipseLinkEntityManager().getDatabaseSession()); dm.initialize(getEclipseLinkEntityManager().getDatabaseSession()); entityDescriptor.addMapping(dm); entityDescriptor.getObjectBuilder().initialize(getEclipseLinkEntityManager().getDatabaseSession());