/** * Create a JPA EntityManager to be used for a transaction. * <p>The default implementation checks whether the EntityManagerFactory * is a Spring proxy and unwraps it first. * @see javax.persistence.EntityManagerFactory#createEntityManager() * @see EntityManagerFactoryInfo#getNativeEntityManagerFactory() */ protected EntityManager createEntityManagerForTransaction() { EntityManagerFactory emf = obtainEntityManagerFactory(); if (emf instanceof EntityManagerFactoryInfo) { emf = ((EntityManagerFactoryInfo) emf).getNativeEntityManagerFactory(); } Map<String, Object> properties = getJpaPropertyMap(); return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); }
/** * Create a container-managed extended EntityManager proxy. * @param emf the EntityManagerFactory to create the EntityManager with. * If this implements the EntityManagerFactoryInfo interface, the corresponding * JpaDialect and PersistenceUnitInfo will be detected accordingly. * @param properties the properties to be passed into the {@code createEntityManager} * call (may be {@code null}) * @param synchronizedWithTransaction whether to automatically join ongoing * transactions (according to the JPA 2.1 SynchronizationType rules) * @return a container-managed EntityManager that expects container-driven lifecycle * management but may opt out of automatic transaction synchronization * @since 4.0 * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map) */ public static EntityManager createContainerManagedEntityManager( EntityManagerFactory emf, @Nullable Map<?, ?> properties, boolean synchronizedWithTransaction) { Assert.notNull(emf, "EntityManagerFactory must not be null"); if (emf instanceof EntityManagerFactoryInfo) { EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf; EntityManagerFactory nativeEmf = emfInfo.getNativeEntityManagerFactory(); EntityManager rawEntityManager = (!CollectionUtils.isEmpty(properties) ? nativeEmf.createEntityManager(properties) : nativeEmf.createEntityManager()); return createProxy(rawEntityManager, emfInfo, true, synchronizedWithTransaction); } else { EntityManager rawEntityManager = (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); return createProxy(rawEntityManager, null, null, null, null, true, synchronizedWithTransaction); } }
/** * Create a JPA EntityManager to be used for a transaction. * <p>The default implementation checks whether the EntityManagerFactory * is a Spring proxy and unwraps it first. * @see javax.persistence.EntityManagerFactory#createEntityManager() * @see EntityManagerFactoryInfo#getNativeEntityManagerFactory() */ protected EntityManager createEntityManagerForTransaction() { EntityManagerFactory emf = obtainEntityManagerFactory(); if (emf instanceof EntityManagerFactoryInfo) { emf = ((EntityManagerFactoryInfo) emf).getNativeEntityManagerFactory(); } Map<String, Object> properties = getJpaPropertyMap(); return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); }
@Test public void testCanCastNativeEntityManagerFactoryToEclipseLinkEntityManagerFactoryImpl() { EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory; assertTrue(emfi.getNativeEntityManagerFactory().getClass().getName().endsWith("EntityManagerFactoryImpl")); }
@Test public void testCanCastNativeEntityManagerFactoryToHibernateEntityManagerFactoryImpl() { EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory; assertTrue(emfi.getNativeEntityManagerFactory() instanceof org.hibernate.jpa.HibernateEntityManagerFactory); assertTrue(emfi.getNativeEntityManagerFactory() instanceof SessionFactory); // as of Hibernate 5.2 }
/** * Create a container-managed extended EntityManager proxy. * @param emf the EntityManagerFactory to create the EntityManager with. * If this implements the EntityManagerFactoryInfo interface, the corresponding * JpaDialect and PersistenceUnitInfo will be detected accordingly. * @param properties the properties to be passed into the {@code createEntityManager} * call (may be {@code null}) * @param synchronizedWithTransaction whether to automatically join ongoing * transactions (according to the JPA 2.1 SynchronizationType rules) * @return a container-managed EntityManager that expects container-driven lifecycle * management but may opt out of automatic transaction synchronization * @since 4.0 * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map) */ public static EntityManager createContainerManagedEntityManager( EntityManagerFactory emf, @Nullable Map<?, ?> properties, boolean synchronizedWithTransaction) { Assert.notNull(emf, "EntityManagerFactory must not be null"); if (emf instanceof EntityManagerFactoryInfo) { EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf; EntityManagerFactory nativeEmf = emfInfo.getNativeEntityManagerFactory(); EntityManager rawEntityManager = (!CollectionUtils.isEmpty(properties) ? nativeEmf.createEntityManager(properties) : nativeEmf.createEntityManager()); return createProxy(rawEntityManager, emfInfo, true, synchronizedWithTransaction); } else { EntityManager rawEntityManager = (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); return createProxy(rawEntityManager, null, null, null, null, true, synchronizedWithTransaction); } }
@Test public void testEntityManagerFactoryImplementsEntityManagerFactoryInfo() { assertTrue("Must have introduced config interface", entityManagerFactory instanceof EntityManagerFactoryInfo); EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory; assertEquals("Person", emfi.getPersistenceUnitName()); assertNotNull("PersistenceUnitInfo must be available", emfi.getPersistenceUnitInfo()); assertNotNull("Raw EntityManagerFactory must be available", emfi.getNativeEntityManagerFactory()); }
@Test public void testEntityManagerFactoryImplementsEntityManagerFactoryInfo() { assertTrue("Must have introduced config interface", this.entityManagerFactory instanceof EntityManagerFactoryInfo); EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) this.entityManagerFactory; assertEquals("Drivers", emfi.getPersistenceUnitName()); assertNotNull("PersistenceUnitInfo must be available", emfi.getPersistenceUnitInfo()); assertNotNull("Raw EntityManagerFactory must be available", emfi.getNativeEntityManagerFactory()); }
protected void checkInvariants(AbstractEntityManagerFactoryBean demf) { assertTrue(EntityManagerFactory.class.isAssignableFrom(demf.getObjectType())); Object gotObject = demf.getObject(); assertTrue("Object created by factory implements EntityManagerFactoryInfo", gotObject instanceof EntityManagerFactoryInfo); EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) demf.getObject(); assertSame("Successive invocations of getObject() return same object", emfi, demf.getObject()); assertSame(emfi, demf.getObject()); assertSame(emfi.getNativeEntityManagerFactory(), mockEmf); }
/** * Extracts the native entity manager factory from a managed Spring one. If Spring * has not wrapped the factory, will return it as is. * * @param entityManagerFactory The (possibly) managed Spring entity manager factory * @return The native entity manager factory * @throws JpaGpsDeviceException */ public EntityManagerFactory extractNative(EntityManagerFactory entityManagerFactory) throws JpaGpsDeviceException { if (entityManagerFactory instanceof EntityManagerFactoryInfo) { return ((EntityManagerFactoryInfo) entityManagerFactory).getNativeEntityManagerFactory(); } return entityManagerFactory; }
public JPAPerister(List<State<T>> states, State<T> startState, Class<T> clazz, EntityManagerFactoryInfo entityManagerFactory, PlatformTransactionManager transactionManager) { this(states, null, startState, clazz, entityManagerFactory.getNativeEntityManagerFactory().createEntityManager(), transactionManager); }
private EntityManager createEntityManager() { EntityManagerFactory entityManagerFactory = transactionManager.getEntityManagerFactory(); if (entityManagerFactory instanceof EntityManagerFactoryInfo) { entityManagerFactory = ((EntityManagerFactoryInfo) entityManagerFactory).getNativeEntityManagerFactory(); } Map<String, Object> properties = transactionManager.getJpaPropertyMap(); return !CollectionUtils.isEmpty(properties) ? entityManagerFactory.createEntityManager(properties) : entityManagerFactory.createEntityManager(); } }
/** * Create a JPA EntityManager to be used for a transaction. * <p>The default implementation checks whether the EntityManagerFactory * is a Spring proxy and unwraps it first. * @see javax.persistence.EntityManagerFactory#createEntityManager() * @see EntityManagerFactoryInfo#getNativeEntityManagerFactory() */ protected EntityManager createEntityManagerForTransaction() { EntityManagerFactory emf = obtainEntityManagerFactory(); if (emf instanceof EntityManagerFactoryInfo) { emf = ((EntityManagerFactoryInfo) emf).getNativeEntityManagerFactory(); } Map<String, Object> properties = getJpaPropertyMap(); return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); }
/** * Create a container-managed extended EntityManager proxy. * @param emf the EntityManagerFactory to create the EntityManager with. * If this implements the EntityManagerFactoryInfo interface, the corresponding * JpaDialect and PersistenceUnitInfo will be detected accordingly. * @param properties the properties to be passed into the {@code createEntityManager} * call (may be {@code null}) * @param synchronizedWithTransaction whether to automatically join ongoing * transactions (according to the JPA 2.1 SynchronizationType rules) * @return a container-managed EntityManager that expects container-driven lifecycle * management but may opt out of automatic transaction synchronization * @since 4.0 * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map) */ public static EntityManager createContainerManagedEntityManager( EntityManagerFactory emf, @Nullable Map<?, ?> properties, boolean synchronizedWithTransaction) { Assert.notNull(emf, "EntityManagerFactory must not be null"); if (emf instanceof EntityManagerFactoryInfo) { EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf; EntityManagerFactory nativeEmf = emfInfo.getNativeEntityManagerFactory(); EntityManager rawEntityManager = (!CollectionUtils.isEmpty(properties) ? nativeEmf.createEntityManager(properties) : nativeEmf.createEntityManager()); return createProxy(rawEntityManager, emfInfo, true, synchronizedWithTransaction); } else { EntityManager rawEntityManager = (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); return createProxy(rawEntityManager, null, null, null, null, true, synchronizedWithTransaction); } }
@Override protected EntityManager getEntityManager(Command<?> command) { if (sharedEntityManager != null) { return sharedEntityManager; } EntityManagerHolder emHolder = ((EntityManagerHolder) TransactionSynchronizationManager.getResource("cmdEM")); if (emHolder != null) { EntityManager em = emHolder.getEntityManager(); EntityManagerFactory nativeEmf = ((EntityManagerFactoryInfo) emf).getNativeEntityManagerFactory(); if (em != null && em.isOpen() && em.getEntityManagerFactory().equals(nativeEmf)) { return em; } } return super.getEntityManager(command); } }
@Override protected EntityManager getEntityManager(Command<?> command) { if (sharedEntityManager != null) { return sharedEntityManager; } EntityManagerHolder emHolder = ((EntityManagerHolder) TransactionSynchronizationManager.getResource("cmdEM")); if (emHolder != null) { EntityManager em = emHolder.getEntityManager(); EntityManagerFactory nativeEmf = ((EntityManagerFactoryInfo) emf).getNativeEntityManagerFactory(); if (em != null && em.isOpen() && em.getEntityManagerFactory().equals(nativeEmf)) { return em; } } return super.getEntityManager(command); } }