use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.
the class SuperclassCollectionTest method createPerson.
private PersonBaseBase createPerson(PersonBaseBase person, String address, String localAddress) {
EntityManager em = createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
person.addresses.add(new Address(address));
person.getLocalAddresses().add(new Address(localAddress));
person = em.merge(person);
tx.commit();
return person;
}
use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.
the class DeleteOrphanTest method testDeleteOrphan.
@Test
public void testDeleteOrphan() throws Exception {
EntityTransaction tx;
EntityManager em = getOrCreateEntityManager();
tx = em.getTransaction();
tx.begin();
Troop disney = new Troop();
disney.setName("Disney");
Soldier mickey = new Soldier();
mickey.setName("Mickey");
disney.addSoldier(mickey);
em.persist(disney);
tx.commit();
em.close();
em = getOrCreateEntityManager();
tx = em.getTransaction();
tx.begin();
Troop troop = em.find(Troop.class, disney.getId());
Hibernate.initialize(troop.getSoldiers());
tx.commit();
em.close();
Soldier soldier = troop.getSoldiers().iterator().next();
troop.getSoldiers().remove(soldier);
troop = (Troop) deserialize(serialize(troop));
em = getOrCreateEntityManager();
tx = em.getTransaction();
tx.begin();
em.merge(troop);
tx.commit();
em.close();
em = getOrCreateEntityManager();
tx = em.getTransaction();
tx.begin();
soldier = em.find(Soldier.class, mickey.getId());
Assert.assertNull("delete-orphan should work", soldier);
troop = em.find(Troop.class, disney.getId());
em.remove(troop);
tx.commit();
em.close();
}
use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.
the class CascadeWithFkConstraintTestTask method execute.
public void execute() {
// Remove garage
EntityManager em = getFactory().createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Garage toRemoveGarage = em.find(Garage.class, garageId);
em.remove(toRemoveGarage);
tx.commit();
em.close();
// Check if there is no garage but cars are still present
EntityManager testEm = getFactory().createEntityManager();
tx = testEm.getTransaction();
tx.begin();
Garage foundGarage = testEm.find(Garage.class, garageId);
Assert.assertNull(foundGarage);
Car foundCar1 = testEm.find(Car.class, car1Id);
Assert.assertEquals(car1Id, foundCar1.id);
Car foundCar2 = testEm.find(Car.class, car2Id);
Assert.assertEquals(car2Id, foundCar2.id);
tx.commit();
testEm.close();
}
use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.
the class AutoFlushTest method testFlushAutoCommit.
@Test
public void testFlushAutoCommit() {
EntityManager entityManager = null;
EntityTransaction txn = null;
try {
//tag::flushing-auto-flush-commit-example[]
entityManager = entityManagerFactory().createEntityManager();
txn = entityManager.getTransaction();
txn.begin();
Person person = new Person("John Doe");
entityManager.persist(person);
log.info("Entity is in persisted state");
txn.commit();
//end::flushing-auto-flush-commit-example[]
} catch (RuntimeException e) {
if (txn != null && txn.isActive())
txn.rollback();
throw e;
} finally {
if (entityManager != null) {
entityManager.close();
}
}
}
use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.
the class BatchTest method withBatch.
private void withBatch() {
int entityCount = 100;
//tag::batch-session-batch-insert-example[]
EntityManager entityManager = null;
EntityTransaction txn = null;
try {
entityManager = entityManagerFactory().createEntityManager();
txn = entityManager.getTransaction();
txn.begin();
int batchSize = 25;
for (int i = 0; i < entityCount; ++i) {
Person Person = new Person(String.format("Person %d", i));
entityManager.persist(Person);
if (i > 0 && i % batchSize == 0) {
//flush a batch of inserts and release memory
entityManager.flush();
entityManager.clear();
}
}
txn.commit();
} catch (RuntimeException e) {
if (txn != null && txn.isActive())
txn.rollback();
throw e;
} finally {
if (entityManager != null) {
entityManager.close();
}
}
//end::batch-session-batch-insert-example[]
}
Aggregations