Search in sources :

Example 31 with EntityManagerFactory

use of jakarta.persistence.EntityManagerFactory in project spring-framework by spring-projects.

the class LocalContainerEntityManagerFactoryBean method createNativeEntityManagerFactory.

@Override
protected EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceException {
    Assert.state(this.persistenceUnitInfo != null, "PersistenceUnitInfo not initialized");
    PersistenceProvider provider = getPersistenceProvider();
    if (provider == null) {
        String providerClassName = this.persistenceUnitInfo.getPersistenceProviderClassName();
        if (providerClassName == null) {
            throw new IllegalArgumentException("No PersistenceProvider specified in EntityManagerFactory configuration, " + "and chosen PersistenceUnitInfo does not specify a provider class name either");
        }
        Class<?> providerClass = ClassUtils.resolveClassName(providerClassName, getBeanClassLoader());
        provider = (PersistenceProvider) BeanUtils.instantiateClass(providerClass);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Building JPA container EntityManagerFactory for persistence unit '" + this.persistenceUnitInfo.getPersistenceUnitName() + "'");
    }
    EntityManagerFactory emf = provider.createContainerEntityManagerFactory(this.persistenceUnitInfo, getJpaPropertyMap());
    postProcessEntityManagerFactory(emf, this.persistenceUnitInfo);
    return emf;
}
Also used : PersistenceProvider(jakarta.persistence.spi.PersistenceProvider) EntityManagerFactory(jakarta.persistence.EntityManagerFactory)

Example 32 with EntityManagerFactory

use of jakarta.persistence.EntityManagerFactory in project spring-framework by spring-projects.

the class SharedEntityManagerCreatorTests method deferredQueryWithResultStream.

@Test
public void deferredQueryWithResultStream() {
    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").getResultStream();
    verify(query).getResultStream();
    verify(targetEm).close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) StoredProcedureQuery(jakarta.persistence.StoredProcedureQuery) Query(jakarta.persistence.Query) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.jupiter.api.Test)

Example 33 with EntityManagerFactory

use of jakarta.persistence.EntityManagerFactory in project spring-framework by spring-projects.

the class SharedEntityManagerCreatorTests method transactionRequiredExceptionOnRefresh.

@Test
public void transactionRequiredExceptionOnRefresh() {
    EntityManagerFactory emf = mock(EntityManagerFactory.class);
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
    assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(() -> em.refresh(new Object()));
}
Also used : EntityManager(jakarta.persistence.EntityManager) TransactionRequiredException(jakarta.persistence.TransactionRequiredException) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.jupiter.api.Test)

Example 34 with EntityManagerFactory

use of jakarta.persistence.EntityManagerFactory in project spring-framework by spring-projects.

the class SharedEntityManagerCreatorTests method proxyingWorksIfInfoReturnsNullEntityManagerInterface.

@Test
public void proxyingWorksIfInfoReturnsNullEntityManagerInterface() {
    EntityManagerFactory emf = mock(EntityManagerFactory.class, withSettings().extraInterfaces(EntityManagerFactoryInfo.class));
    // EntityManagerFactoryInfo.getEntityManagerInterface returns null
    assertThat(SharedEntityManagerCreator.createSharedEntityManager(emf)).isNotNull();
}
Also used : EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.jupiter.api.Test)

Aggregations

EntityManagerFactory (jakarta.persistence.EntityManagerFactory)34 Test (org.junit.jupiter.api.Test)27 EntityManager (jakarta.persistence.EntityManager)25 StoredProcedureQuery (jakarta.persistence.StoredProcedureQuery)6 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)6 Query (jakarta.persistence.Query)4 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)4 EntityTransaction (jakarta.persistence.EntityTransaction)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 EntityManagerHolder (org.springframework.orm.jpa.EntityManagerHolder)3 PersistenceException (jakarta.persistence.PersistenceException)2 AsyncEvent (jakarta.servlet.AsyncEvent)2 AsyncListener (jakarta.servlet.AsyncListener)2 FilterChain (jakarta.servlet.FilterChain)2 Callable (java.util.concurrent.Callable)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2