Search in sources :

Example 41 with EntityTransaction

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;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager)

Example 42 with EntityTransaction

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();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 43 with EntityTransaction

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();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager)

Example 44 with EntityTransaction

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();
        }
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 45 with EntityTransaction

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[]
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Person(org.hibernate.userguide.model.Person)

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)59 EntityManager (javax.persistence.EntityManager)38 Test (org.junit.Test)12 EntityManagerFactory (javax.persistence.EntityManagerFactory)9 Query (javax.persistence.Query)8 UserTestDbo (org.webpieces.plugins.hibernate.app.dbo.UserTestDbo)6 InumSqlEntry (org.gluu.oxtrust.model.sql.InumSqlEntry)4 Person (org.hibernate.userguide.model.Person)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 Transactional (com.google.inject.persist.Transactional)2 Priority (org.hibernate.envers.test.Priority)2 DefaultTransactionAttribute (org.springframework.transaction.interceptor.DefaultTransactionAttribute)2 UserDbo (WEBPIECESxPACKAGE.base.libs.UserDbo)1 UserRole (WEBPIECESxPACKAGE.base.libs.UserRole)1 FileType (aserg.gtf.model.FileType)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1