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