Search in sources :

Example 51 with EntityManager

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

the class ContainerManagedEntityManagerIntegrationTests method testEntityManagerProxyIsProxy.

@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
    EntityManager em = createContainerManagedEntityManager();
    assertThat(Proxy.isProxyClass(em.getClass())).isTrue();
    Query q = em.createQuery("select p from Person as p");
    List<Person> people = q.getResultList();
    assertThat(people.isEmpty()).isTrue();
    assertThat(em.isOpen()).as("Should be open to start with").isTrue();
    assertThatIllegalStateException().as("Close should not work on container managed EM").isThrownBy(em::close);
    assertThat(em.isOpen()).isTrue();
}
Also used : EntityManager(jakarta.persistence.EntityManager) Query(jakarta.persistence.Query) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.jupiter.api.Test)

Example 52 with EntityManager

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

the class HibernateEntityManagerFactoryIntegrationTests method testCanUnwrapAopProxy.

@Test
public void testCanUnwrapAopProxy() {
    EntityManager em = entityManagerFactory.createEntityManager();
    EntityManager proxy = ProxyFactory.getProxy(EntityManager.class, new SingletonTargetSource(em));
    boolean condition = em instanceof org.hibernate.jpa.HibernateEntityManager;
    assertThat(condition).isTrue();
    boolean condition1 = proxy instanceof org.hibernate.jpa.HibernateEntityManager;
    assertThat(condition1).isFalse();
    assertThat(proxy.unwrap(org.hibernate.jpa.HibernateEntityManager.class) != null).isTrue();
    assertThat(proxy.unwrap(org.hibernate.jpa.HibernateEntityManager.class)).isSameAs(em);
    assertThat(proxy.getDelegate()).isSameAs(em.getDelegate());
}
Also used : SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) EntityManager(jakarta.persistence.EntityManager) Test(org.junit.jupiter.api.Test)

Example 53 with EntityManager

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

the class LocalContainerEntityManagerFactoryBeanTests method testApplicationManagedEntityManagerWithTransactionAndCommitException.

@Test
public void testApplicationManagedEntityManagerWithTransactionAndCommitException() throws Exception {
    Object testEntity = new Object();
    EntityTransaction mockTx = mock(EntityTransaction.class);
    willThrow(new OptimisticLockException()).given(mockTx).commit();
    // This one's for the tx (shared)
    EntityManager sharedEm = mock(EntityManager.class);
    given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
    // This is the application-specific one
    EntityManager mockEm = mock(EntityManager.class);
    given(mockEm.getTransaction()).willReturn(mockTx);
    given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
    LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
    JpaTransactionManager jpatm = new JpaTransactionManager();
    jpatm.setEntityManagerFactory(cefb.getObject());
    TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
    EntityManagerFactory emf = cefb.getObject();
    assertThat(cefb.getObject()).as("EntityManagerFactory reference must be cached after init").isSameAs(emf);
    assertThat(emf).as("EMF must be proxied").isNotSameAs(mockEmf);
    EntityManager em = emf.createEntityManager();
    em.joinTransaction();
    assertThat(em.contains(testEntity)).isFalse();
    assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() -> jpatm.commit(txStatus));
    cefb.destroy();
    verify(mockTx).begin();
    verify(mockEm).contains(testEntity);
    verify(mockEmf).close();
}
Also used : EntityTransaction(jakarta.persistence.EntityTransaction) EntityManager(jakarta.persistence.EntityManager) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) DefaultTransactionAttribute(org.springframework.transaction.interceptor.DefaultTransactionAttribute) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) OptimisticLockException(jakarta.persistence.OptimisticLockException) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.jupiter.api.Test)

Example 54 with EntityManager

use of jakarta.persistence.EntityManager 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 55 with EntityManager

use of jakarta.persistence.EntityManager 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)

Aggregations

EntityManager (jakarta.persistence.EntityManager)55 Test (org.junit.jupiter.api.Test)52 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)25 Query (jakarta.persistence.Query)9 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)9 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)7 Person (org.springframework.orm.jpa.domain.Person)7 StoredProcedureQuery (jakarta.persistence.StoredProcedureQuery)6 Properties (java.util.Properties)5 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)5 EntityManagerHolder (org.springframework.orm.jpa.EntityManagerHolder)5 EntityTransaction (jakarta.persistence.EntityTransaction)4 HashMap (java.util.HashMap)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 ExpectedLookupTemplate (org.springframework.context.testfixture.jndi.ExpectedLookupTemplate)4 TransactionStatus (org.springframework.transaction.TransactionStatus)4 NoResultException (jakarta.persistence.NoResultException)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 AfterEach (org.junit.jupiter.api.AfterEach)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3