Search in sources :

Example 1 with EntityTransaction

use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.

the class EmbeddableWithDeclaredDataTest method initData.

@Test
@Priority(10)
public void initData() {
    EntityManager em = getEntityManager();
    EntityWithEmbeddableWithDeclaredData entity = new EntityWithEmbeddableWithDeclaredData();
    entity.setName("Entity 1");
    entity.setValue(new EmbeddableWithDeclaredData(42, "TestCodeart"));
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.persist(entity);
    tx.commit();
    em.close();
    id = entity.getId();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) Priority(org.hibernate.envers.test.Priority)

Example 2 with EntityTransaction

use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.

the class TransactionUtil method doInJPA.

/**
	 * Execute function in a JPA transaction
	 *
	 * @param factorySupplier EntityManagerFactory supplier
	 * @param function function
	 * @param <T> result type
	 *
	 * @return result
	 */
public static <T> T doInJPA(Supplier<EntityManagerFactory> factorySupplier, JPATransactionFunction<T> function) {
    T result = null;
    EntityManager entityManager = null;
    EntityTransaction txn = null;
    try {
        entityManager = factorySupplier.get().createEntityManager();
        function.beforeTransactionCompletion();
        txn = entityManager.getTransaction();
        txn.begin();
        result = function.apply(entityManager);
        txn.commit();
    } catch (Throwable e) {
        if (txn != null && txn.isActive()) {
            txn.rollback();
        }
        throw e;
    } finally {
        function.afterTransactionCompletion();
        if (entityManager != null) {
            entityManager.close();
        }
    }
    return result;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager)

Example 3 with EntityTransaction

use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.

the class TransactionUtil method doInJPA.

/**
	 * Execute function in a JPA transaction without return value
	 *
	 * @param factorySupplier EntityManagerFactory supplier
	 * @param function function
	 */
public static void doInJPA(Supplier<EntityManagerFactory> factorySupplier, JPATransactionVoidFunction function) {
    EntityManager entityManager = null;
    EntityTransaction txn = null;
    try {
        entityManager = factorySupplier.get().createEntityManager();
        function.beforeTransactionCompletion();
        txn = entityManager.getTransaction();
        txn.begin();
        function.accept(entityManager);
        txn.commit();
    } catch (Throwable e) {
        if (txn != null && txn.isActive()) {
            txn.rollback();
        }
        throw e;
    } finally {
        function.afterTransactionCompletion();
        if (entityManager != null) {
            entityManager.close();
        }
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager)

Example 4 with EntityTransaction

use of javax.persistence.EntityTransaction in project spring-framework by spring-projects.

the class JpaTransactionManager method doCommit.

@Override
protected void doCommit(DefaultTransactionStatus status) {
    JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
    if (status.isDebug()) {
        logger.debug("Committing JPA transaction on EntityManager [" + txObject.getEntityManagerHolder().getEntityManager() + "]");
    }
    try {
        EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
        tx.commit();
    } catch (RollbackException ex) {
        if (ex.getCause() instanceof RuntimeException) {
            DataAccessException dex = getJpaDialect().translateExceptionIfPossible((RuntimeException) ex.getCause());
            if (dex != null) {
                throw dex;
            }
        }
        throw new TransactionSystemException("Could not commit JPA transaction", ex);
    } catch (RuntimeException ex) {
        // Assumably failed to flush changes to database.
        throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect());
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.persistence.RollbackException) DataAccessException(org.springframework.dao.DataAccessException)

Example 5 with EntityTransaction

use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.

the class TreatKeywordTest method testTreatWithRestrictionOnAbstractClass.

@Test
@TestForIssue(jiraKey = "HHH-9411")
public void testTreatWithRestrictionOnAbstractClass() {
    EntityManager em = getOrCreateEntityManager();
    EntityTransaction entityTransaction = em.getTransaction();
    entityTransaction.begin();
    Greyhound greyhound = new Greyhound();
    Dachshund dachshund = new Dachshund();
    em.persist(greyhound);
    em.persist(dachshund);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TreatAnimal> criteriaQuery = cb.createQuery(TreatAnimal.class);
    Root<TreatAnimal> animal = criteriaQuery.from(TreatAnimal.class);
    Root<Dog> dog = cb.treat(animal, Dog.class);
    // only fast dogs
    criteriaQuery.where(cb.isTrue(dog.<Boolean>get("fast")));
    List<TreatAnimal> results = em.createQuery(criteriaQuery).getResultList();
    // we should only have a single Greyhound here, not slow long dogs!
    assertEquals(Arrays.asList(greyhound), results);
    entityTransaction.commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)540 EntityManager (javax.persistence.EntityManager)447 Query (javax.persistence.Query)143 Person (org.datanucleus.samples.annotations.models.company.Person)101 List (java.util.List)90 TypedQuery (javax.persistence.TypedQuery)82 NoResultException (javax.persistence.NoResultException)77 ArrayList (java.util.ArrayList)69 NotFoundException (org.opencastproject.util.NotFoundException)61 PersistenceException (javax.persistence.PersistenceException)59 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)55 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)50 JPAQuery (org.datanucleus.api.jpa.JPAQuery)32 EntityManagerFactory (javax.persistence.EntityManagerFactory)29 Predicate (javax.persistence.criteria.Predicate)26 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)21 Connection (java.sql.Connection)20 RollbackException (javax.persistence.RollbackException)20 HashSet (java.util.HashSet)19 Employee (org.datanucleus.samples.annotations.models.company.Employee)19