/** * Create a transactional EntityManager proxy for the given EntityManagerFactory. * @param emf the EntityManagerFactory to delegate to. * @return a shareable transaction EntityManager proxy */ public static EntityManager createSharedEntityManager(EntityManagerFactory emf) { return createSharedEntityManager(emf, null, true); }
/** * Create a transactional EntityManager proxy for the given EntityManagerFactory. * @param emf the EntityManagerFactory to delegate to. * @param properties the properties to be passed into the * {@code createEntityManager} call (may be {@code null}) * @return a shareable transaction EntityManager proxy */ public static EntityManager createSharedEntityManager(EntityManagerFactory emf, @Nullable Map<?, ?> properties) { return createSharedEntityManager(emf, properties, true); }
/** * Create a transactional EntityManager proxy for the given EntityManagerFactory. * @param emf the EntityManagerFactory to obtain EntityManagers from as needed * @param properties the properties to be passed into the * {@code createEntityManager} call (may be {@code null}) * @param entityManagerInterfaces the interfaces to be implemented by the * EntityManager. Allows the addition or specification of proprietary interfaces. * @return a shareable transactional EntityManager proxy */ public static EntityManager createSharedEntityManager( EntityManagerFactory emf, @Nullable Map<?, ?> properties, Class<?>... entityManagerInterfaces) { return createSharedEntityManager(emf, properties, true, entityManagerInterfaces); }
/** * Create a transactional EntityManager proxy for the given EntityManagerFactory. * @param emf the EntityManagerFactory to delegate to. * @return a shareable transaction EntityManager proxy */ public static EntityManager createSharedEntityManager(EntityManagerFactory emf) { return createSharedEntityManager(emf, null, true); }
/** * Create a transactional EntityManager proxy for the given EntityManagerFactory. * @param emf the EntityManagerFactory to delegate to. * @param properties the properties to be passed into the * {@code createEntityManager} call (may be {@code null}) * @return a shareable transaction EntityManager proxy */ public static EntityManager createSharedEntityManager(EntityManagerFactory emf, @Nullable Map<?, ?> properties) { return createSharedEntityManager(emf, properties, true); }
/** * Create a transactional EntityManager proxy for the given EntityManagerFactory. * @param emf the EntityManagerFactory to obtain EntityManagers from as needed * @param properties the properties to be passed into the * {@code createEntityManager} call (may be {@code null}) * @param entityManagerInterfaces the interfaces to be implemented by the * EntityManager. Allows the addition or specification of proprietary interfaces. * @return a shareable transactional EntityManager proxy */ public static EntityManager createSharedEntityManager( EntityManagerFactory emf, @Nullable Map<?, ?> properties, Class<?>... entityManagerInterfaces) { return createSharedEntityManager(emf, properties, true, entityManagerInterfaces); }
/** * Create a transactional EntityManager proxy for the given EntityManagerFactory. * @param emf the EntityManagerFactory to delegate to. * @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 shareable transaction EntityManager proxy * @since 4.0 */ public static EntityManager createSharedEntityManager( EntityManagerFactory emf, @Nullable Map<?, ?> properties, boolean synchronizedWithTransaction) { Class<?> emIfc = (emf instanceof EntityManagerFactoryInfo ? ((EntityManagerFactoryInfo) emf).getEntityManagerInterface() : EntityManager.class); return createSharedEntityManager(emf, properties, synchronizedWithTransaction, (emIfc == null ? NO_ENTITY_MANAGER_INTERFACES : new Class<?>[] {emIfc})); }
@Autowired public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) { this.entityManagerFactory = entityManagerFactory; this.sharedEntityManager = SharedEntityManagerCreator.createSharedEntityManager(this.entityManagerFactory); }
@Test(expected = TransactionRequiredException.class) public void transactionRequiredExceptionOnMerge() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.merge(new Object()); }
/** * Create a transactional EntityManager proxy for the given EntityManagerFactory. * @param emf the EntityManagerFactory to delegate to. * @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 shareable transaction EntityManager proxy * @since 4.0 */ public static EntityManager createSharedEntityManager( EntityManagerFactory emf, @Nullable Map<?, ?> properties, boolean synchronizedWithTransaction) { Class<?> emIfc = (emf instanceof EntityManagerFactoryInfo ? ((EntityManagerFactoryInfo) emf).getEntityManagerInterface() : EntityManager.class); return createSharedEntityManager(emf, properties, synchronizedWithTransaction, (emIfc == null ? NO_ENTITY_MANAGER_INTERFACES : new Class<?>[] {emIfc})); }
@Test(expected = TransactionRequiredException.class) public void transactionRequiredExceptionOnFlush() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.flush(); }
@Test(expected = TransactionRequiredException.class) public void transactionRequiredExceptionOnPersist() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.persist(new Object()); }
@Test(expected = TransactionRequiredException.class) public void transactionRequiredExceptionOnRemove() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.remove(new Object()); }
private EntityManager resolveEntityManager(@Nullable String requestingBeanName) { // Obtain EntityManager reference from JNDI? EntityManager em = getPersistenceContext(this.unitName, false); if (em == null) { // No pre-built EntityManager found -> build one based on factory. // Obtain EntityManagerFactory from JNDI? EntityManagerFactory emf = getPersistenceUnit(this.unitName); if (emf == null) { // Need to search for EntityManagerFactory beans. emf = findEntityManagerFactory(this.unitName, requestingBeanName); } // Inject a shared transactional EntityManager proxy. if (emf instanceof EntityManagerFactoryInfo && ((EntityManagerFactoryInfo) emf).getEntityManagerInterface() != null) { // Create EntityManager based on the info's vendor-specific type // (which might be more specific than the field's type). em = SharedEntityManagerCreator.createSharedEntityManager( emf, this.properties, this.synchronizedWithTransaction); } else { // Create EntityManager based on the field's type. em = SharedEntityManagerCreator.createSharedEntityManager( emf, this.properties, this.synchronizedWithTransaction, getResourceType()); } } return em; }
@Override public final void afterPropertiesSet() { EntityManagerFactory emf = getEntityManagerFactory(); if (emf == null) { throw new IllegalArgumentException("'entityManagerFactory' or 'persistenceUnitName' is required"); } if (emf instanceof EntityManagerFactoryInfo) { EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf; if (this.entityManagerInterface == null) { this.entityManagerInterface = emfInfo.getEntityManagerInterface(); if (this.entityManagerInterface == null) { this.entityManagerInterface = EntityManager.class; } } } else { if (this.entityManagerInterface == null) { this.entityManagerInterface = EntityManager.class; } } this.shared = SharedEntityManagerCreator.createSharedEntityManager( emf, getJpaPropertyMap(), this.synchronizedWithTransaction, this.entityManagerInterface); }
@Test(expected = TransactionRequiredException.class) public void transactionRequiredExceptionOnRefresh() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.refresh(new Object()); }
@Test(expected = TransactionRequiredException.class) public void transactionRequiredExceptionOnJoinTransaction() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.joinTransaction(); }
@Test public void deferredQueryWithResultList() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager targetEm = mock(EntityManager.class); Query query = mock(Query.class); given(emf.createEntityManager()).willReturn(targetEm); given(targetEm.createQuery("x")).willReturn(query); given(targetEm.isOpen()).willReturn(true); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.createQuery("x").getResultList(); verify(query).getResultList(); verify(targetEm).close(); }
@Test public void deferredQueryWithUpdate() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager targetEm = mock(EntityManager.class); Query query = mock(Query.class); given(emf.createEntityManager()).willReturn(targetEm); given(targetEm.createQuery("x")).willReturn(query); given(targetEm.isOpen()).willReturn(true); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.createQuery("x").executeUpdate(); verify(query).executeUpdate(); verify(targetEm).close(); }
@Test public void deferredQueryWithSingleResult() { EntityManagerFactory emf = mock(EntityManagerFactory.class); EntityManager targetEm = mock(EntityManager.class); Query query = mock(Query.class); given(emf.createEntityManager()).willReturn(targetEm); given(targetEm.createQuery("x")).willReturn(query); given(targetEm.isOpen()).willReturn(true); EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); em.createQuery("x").getSingleResult(); verify(query).getSingleResult(); verify(targetEm).close(); }