Search in sources :

Example 71 with EntityTransaction

use of javax.persistence.EntityTransaction in project joynr by bmwcarit.

the class BounceProxyDatabase method updateChannelAssignment.

@Override
public void updateChannelAssignment(String ccid, BounceProxyInformation bpInfo) throws IllegalArgumentException {
    logger.trace("updateChannelAssignment(ccid={}, bpId={})", ccid, bpInfo.getId());
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        BounceProxyEntity bpEntity = em.find(BounceProxyEntity.class, bpInfo.getId());
        if (bpEntity == null) {
            logger.error("No bounce proxy with ID {} registered", bpInfo.getId());
            throw new IllegalArgumentException("No bounce proxy with ID '" + bpInfo.getId() + "' registered");
        }
        ChannelEntity channelEntity = em.find(ChannelEntity.class, ccid);
        if (channelEntity == null) {
            logger.error("No channel with ID {} was registered before.", ccid);
            throw new IllegalArgumentException("No channel with ID '" + ccid + "' was registered before.");
        }
        // TODO what to do if channel with the same ID is added, but
        // different other properties?
        bpEntity.addChannel(channelEntity);
        em.merge(bpEntity);
        tx.commit();
    } catch (RuntimeException ex) {
        logger.error("Channel assignment could not be persisted. error: {}", ex.getMessage());
        if (tx != null && tx.isActive())
            tx.rollback();
        throw ex;
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) BounceProxyEntity(io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.BounceProxyEntity) ChannelEntity(io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.ChannelEntity)

Example 72 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class MultithreadedTest method persistFarmWithAnimals.

/**
 * Convenience method to create a Farm with 100 Animals. Used by various tests in this file
 * @return The id of the Farm object
 */
private Object persistFarmWithAnimals() {
    LOG.debug(">> Persisting data");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    Object farmId = "Jones Farm";
    try {
        tx.begin();
        Farm farm = new Farm("Jones Farm");
        em.persist(farm);
        for (int i = 0; i < 100; i++) {
            Animal an = new Animal("Sheep" + i);
            an.setFarm(farm);
            farm.getAnimals().add(an);
            em.persist(an);
        }
        tx.commit();
    } catch (Throwable thr) {
        LOG.error("Exception persisting objects", thr);
        fail("Exception persisting data : " + thr.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    LOG.debug(">> Persisted data");
    // Verify the persistence
    em = emf.createEntityManager();
    tx = em.getTransaction();
    try {
        tx.begin();
        LOG.debug(">> Querying Animals");
        Query q = em.createQuery("SELECT a FROM " + Animal.class.getName() + " a");
        List<Animal> ans = q.getResultList();
        for (Animal a : ans) {
            LOG.debug(">> animal=" + a + " farm=" + a.getFarm());
        }
        LOG.debug(">> Queried Animals");
        tx.commit();
    } catch (Throwable thr) {
        LOG.error("Exception checking objects", thr);
        fail("Exception checking data : " + thr.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    return farmId;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Animal(org.datanucleus.samples.annotations.one_many.bidir.Animal) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm)

Example 73 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class RelationshipsTest method testOneToOneUniWithOrphanRemovalAndNulling.

/**
 * Test of 1-1 Uni relation, and use of orphanRemoval
 */
public void testOneToOneUniWithOrphanRemovalAndNulling() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            LoginAccount acct = new LoginAccount(1, "Bill", "Gates");
            Login login = new Login("billy", "$$$$$$");
            login.setId(1);
            acct.setLogin(login);
            em.persist(acct);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while creating Login and LoginAccount");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore, and trigger orphanRemoval by nulling
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            LoginAccount acct = em.find(LoginAccount.class, new Long(1));
            assertEquals("LoginAccount has incorrect firstName", "Bill", acct.getFirstName());
            assertEquals("LoginAccount has incorrect lastName", "Gates", acct.getLastName());
            assertNotNull(acct.getLogin());
            Login login = acct.getLogin();
            assertEquals("Login has incorrect username", "billy", login.getUserName());
            assertEquals("Login has incorrect password", "$$$$$$", login.getPassword());
            // Null the login field so we can trigger orphanRemoval
            acct.setLogin(null);
            em.flush();
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving Login and LoginAccount");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            Login login = em.find(Login.class, new Long(1));
            assertNull("Login should have been deleted but still exists", login);
            LoginAccount acct = em.find(LoginAccount.class, new Long(1));
            assertEquals("LoginAccount has incorrect firstName", "Bill", acct.getFirstName());
            assertEquals("LoginAccount has incorrect lastName", "Gates", acct.getLastName());
            assertNull(acct.getLogin());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving Login and LoginAccount");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(LoginAccount.class);
        clean(Login.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) LoginAccount(org.datanucleus.samples.annotations.one_one.unidir.LoginAccount) Login(org.datanucleus.samples.annotations.one_one.unidir.Login)

Example 74 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class RelationshipsTest method testManyToMany.

/**
 * Test of M-N relations.
 */
public void testManyToMany() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            PetroleumSupplier s = new PetroleumSupplier(101, "Esso");
            PetroleumCustomer c = new PetroleumCustomer(102, "Brians Fuels");
            s.getCustomers().add(c);
            c.getSuppliers().add(s);
            em.persist(c);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while creating Customers and Suppliers");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    // TODO Retrieve the objects and check the persistence
    // TODO Do some updates
    } finally {
        clean(PetroleumSupplier.class);
        clean(PetroleumCustomer.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) PetroleumSupplier(org.datanucleus.samples.annotations.many_many.PetroleumSupplier) EntityManager(javax.persistence.EntityManager) PetroleumCustomer(org.datanucleus.samples.annotations.many_many.PetroleumCustomer)

Example 75 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class RelationshipsTest method testOneToOneUniWithOrphanRemovalAndDeleting.

/**
 * Test of 1-1 Uni relation, and use of orphanRemoval
 */
public void testOneToOneUniWithOrphanRemovalAndDeleting() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            LoginAccount acct = new LoginAccount(1, "Bill", "Gates");
            Login login = new Login("billy", "$$$$$$");
            login.setId(1);
            acct.setLogin(login);
            em.persist(acct);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while creating Login and LoginAccount");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore, and trigger orphanRemoval by nulling
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            LoginAccount acct = em.find(LoginAccount.class, new Long(1));
            assertEquals("LoginAccount has incorrect firstName", "Bill", acct.getFirstName());
            assertEquals("LoginAccount has incorrect lastName", "Gates", acct.getLastName());
            assertNotNull(acct.getLogin());
            Login login = acct.getLogin();
            assertEquals("Login has incorrect username", "billy", login.getUserName());
            assertEquals("Login has incorrect password", "$$$$$$", login.getPassword());
            // Delete the LoginAccount object so we can trigger orphanRemoval
            em.remove(acct);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving Login and LoginAccount and deleting LoginAccount");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            Login login = em.find(Login.class, new Long(1));
            assertNull("Login should have been deleted but still exists", login);
            LoginAccount acct = em.find(LoginAccount.class, new Long(1));
            assertNull("LoginAccount should have been deleted but still exists", acct);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving Login and LoginAccount");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(LoginAccount.class);
        clean(Login.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) LoginAccount(org.datanucleus.samples.annotations.one_one.unidir.LoginAccount) Login(org.datanucleus.samples.annotations.one_one.unidir.Login)

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