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());
}
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 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();
}
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()));
}
Aggregations