use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class LocalContainerEntityManagerFactoryBeanTests method testApplicationManagedEntityManagerWithoutTransaction.
@Test
public void testApplicationManagedEntityManagerWithoutTransaction() throws Exception {
Object testEntity = new Object();
EntityManager mockEm = mock(EntityManager.class);
given(mockEmf.createEntityManager()).willReturn(mockEm);
LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
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();
assertThat(em.contains(testEntity)).isFalse();
cefb.destroy();
verify(mockEmf).close();
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class LocalContainerEntityManagerFactoryBeanTests method testApplicationManagedEntityManagerWithTransaction.
@Test
public void testApplicationManagedEntityManagerWithTransaction() throws Exception {
Object testEntity = new Object();
EntityTransaction mockTx = mock(EntityTransaction.class);
// 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();
jpatm.commit(txStatus);
cefb.destroy();
verify(mockTx).begin();
verify(mockTx).commit();
verify(mockEm).contains(testEntity);
verify(mockEmf).close();
}
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();
}
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();
}
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());
}
Aggregations