Search in sources :

Example 86 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project HotswapAgent by HotswapProjects.

the class Hibernate3JPAHelper method createContainerEntityManagerFactoryProxy.

/**
 * Creates the container entity manager factory proxy.
 *
 * @param info            persistent unit definition
 * @param properties            properties to create entity manager factory
 * @param original            entity manager factory
 * @return proxy of entity manager
 */
public static EntityManagerFactory createContainerEntityManagerFactoryProxy(PersistenceUnitInfo info, Map<?, ?> properties, EntityManagerFactory original) {
    // ensure only once
    if (wrappedPersistenceUnitNames.contains(info.getPersistenceUnitName())) {
        return original;
    }
    wrappedPersistenceUnitNames.add(info.getPersistenceUnitName());
    EntityManagerFactoryProxy wrapper = EntityManagerFactoryProxy.getWrapper(info.getPersistenceUnitName());
    EntityManagerFactory proxy = wrapper.proxy(original, info.getPersistenceUnitName(), info, properties);
    initPlugin(original);
    LOGGER.debug("Returning container EntityManager proxy {} instead of EntityManager {}", proxy.getClass(), original);
    return proxy;
}
Also used : EntityManagerFactoryProxy(org.hotswap.agent.plugin.hibernate3.jpa.proxy.EntityManagerFactoryProxy) EntityManagerFactory(javax.persistence.EntityManagerFactory)

Example 87 with EntityManagerFactory

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

the class RelationshipsTest method testOneToManyJoinTableIndexedList.

/**
 * Test of 1-N JoinTable indexed list (JPA2).
 */
public void testOneToManyJoinTableIndexedList() {
    EntityManagerFactory emf2 = getEMF(1, "JPATest", null);
    try {
        EntityManager em = emf2.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            UserGroup grp = new UserGroup(1, "JDO Expert Group");
            GroupMember member1 = new GroupMember(1, "Craig Russell");
            GroupMember member2 = new GroupMember(2, "David Jordan");
            List<GroupMember> members = new ArrayList<>();
            members.add(member1);
            members.add(member2);
            grp.setMembers(members);
            em.persist(grp);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while creating UserGroup+GroupMembers");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = emf2.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            List result = em.createQuery("SELECT Object(T) FROM " + UserGroup.class.getName() + " T").getResultList();
            assertEquals(1, result.size());
            UserGroup grp = (UserGroup) result.get(0);
            Collection<GroupMember> members = grp.getMembers();
            assertEquals("Number of GroupMembers in UserGroup is incorrect", 2, members.size());
            Iterator<GroupMember> iter = members.iterator();
            boolean hasMem1 = false;
            boolean hasMem2 = false;
            GroupMember mem1 = iter.next();
            GroupMember mem2 = iter.next();
            if (mem1.getName().equals("Craig Russell")) {
                hasMem1 = true;
            }
            if (mem2.getName().equals("David Jordan")) {
                hasMem2 = true;
            }
            assertTrue("First member of user group is not present (in right place)", hasMem1);
            assertTrue("Second member of user group is not present (in right place)", hasMem2);
            tx.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving UserGroup+GroupMembers");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(emf2, UserGroup.class);
        clean(emf2, GroupMember.class);
    }
    emf2.close();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) GroupMember(org.jpox.samples.one_many.unidir_2.GroupMember) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) UserGroup(org.jpox.samples.one_many.unidir_2.UserGroup)

Example 88 with EntityManagerFactory

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

the class CompoundIdentityTest method testOneToOneUniSingleXML.

/**
 * Basic test of 1-1 uni relation using compound identity relation and JPA XML.
 */
public void testOneToOneUniSingleXML() {
    // Swap to "JPATest" EMF
    EntityManagerFactory emf = getEMF(1, "JPATest", null);
    try {
        org.jpox.samples.compoundidentity.CompoundSingleTarget[] targets = new org.jpox.samples.compoundidentity.CompoundSingleTarget[6];
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            org.jpox.samples.compoundidentity.CompoundHolder holder1 = new org.jpox.samples.compoundidentity.CompoundHolder("First Holder");
            org.jpox.samples.compoundidentity.CompoundHolder holder2 = new org.jpox.samples.compoundidentity.CompoundHolder("Second Holder");
            org.jpox.samples.compoundidentity.CompoundHolder holder3 = new org.jpox.samples.compoundidentity.CompoundHolder("Third Holder");
            targets[0] = new org.jpox.samples.compoundidentity.CompoundSingleTarget(holder3, 1.0);
            targets[1] = new org.jpox.samples.compoundidentity.CompoundSingleTarget(holder3, 2.0);
            targets[2] = new org.jpox.samples.compoundidentity.CompoundSingleTarget(holder2, 3.0);
            targets[3] = new org.jpox.samples.compoundidentity.CompoundSingleTarget(holder2, 4.0);
            targets[4] = new org.jpox.samples.compoundidentity.CompoundSingleTarget(holder1, 5.0);
            targets[5] = new org.jpox.samples.compoundidentity.CompoundSingleTarget(holder1, 6.0);
            for (int i = 0; i < 6; i++) {
                em.persist(targets[i]);
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during test", e);
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        em = emf.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            List result = em.createQuery("SELECT Object(T) FROM " + org.jpox.samples.compoundidentity.CompoundHolder.class.getName() + " T WHERE T.name = 'First Holder'").getResultList();
            org.jpox.samples.compoundidentity.CompoundHolder holder1 = (org.jpox.samples.compoundidentity.CompoundHolder) result.get(0);
            result = em.createQuery("SELECT Object(T) FROM " + org.jpox.samples.compoundidentity.CompoundHolder.class.getName() + " T WHERE T.name = 'Second Holder'").getResultList();
            org.jpox.samples.compoundidentity.CompoundHolder holder2 = (org.jpox.samples.compoundidentity.CompoundHolder) result.get(0);
            result = em.createQuery("SELECT Object(T) FROM " + org.jpox.samples.compoundidentity.CompoundHolder.class.getName() + " T WHERE T.name = 'Third Holder'").getResultList();
            org.jpox.samples.compoundidentity.CompoundHolder holder3 = (org.jpox.samples.compoundidentity.CompoundHolder) result.get(0);
            assertEquals("Name of holder was incorrect", "First Holder", holder1.getName());
            assertEquals("Name of holder was incorrect", "Second Holder", holder2.getName());
            assertEquals("Name of holder was incorrect", "Third Holder", holder3.getName());
            for (int i = 0; i < targets.length; i++) {
                result = em.createQuery("SELECT Object(T) FROM " + org.jpox.samples.compoundidentity.CompoundSingleTarget.class.getName() + " T WHERE T.value = " + (float) ((i + 1) * 1.0)).getResultList();
                org.jpox.samples.compoundidentity.CompoundSingleTarget target = (org.jpox.samples.compoundidentity.CompoundSingleTarget) result.get(0);
                assertEquals(i + 1, target.getValue(), 0);
                if (i == 0 || i == 1) {
                    assertEquals("Name of holder of target is incorrect", "Third Holder", target.getHolder().getName());
                }
                if (i == 2 || i == 3) {
                    assertEquals("Name of holder of target is incorrect", "Second Holder", target.getHolder().getName());
                }
                if (i == 4 || i == 5) {
                    assertEquals("Name of holder of target is incorrect", "First Holder", target.getHolder().getName());
                }
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during test", e);
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(emf, org.jpox.samples.compoundidentity.CompoundSingleTarget.class);
        clean(emf, org.jpox.samples.compoundidentity.CompoundHolder.class);
        emf.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) CompoundSingleTarget(org.datanucleus.samples.annotations.compoundidentity.CompoundSingleTarget) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) List(java.util.List) CompoundHolder(org.datanucleus.samples.annotations.compoundidentity.CompoundHolder)

Example 89 with EntityManagerFactory

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

the class JPQLQueryTest method testQueryUsingEntityNameNotYetLoaded.

public void testQueryUsingEntityNameNotYetLoaded() {
    // Swap to "JPATest" EMF
    EntityManagerFactory emf = getEMF(1, "JPATest", null);
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        List result = em.createQuery("SELECT T FROM Person_Ann T").getResultList();
        assertEquals(0, result.size());
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
        emf.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) List(java.util.List) ArrayList(java.util.ArrayList)

Example 90 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project syndesis by syndesisio.

the class ActionTest method setUp.

@BeforeClass
public static void setUp() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("io.syndesis.runtime.db");
    em = factory.createEntityManager();
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) BeforeClass(org.junit.BeforeClass)

Aggregations

EntityManagerFactory (javax.persistence.EntityManagerFactory)302 EntityManager (javax.persistence.EntityManager)103 Test (org.junit.Test)90 HashMap (java.util.HashMap)48 EntityTransaction (javax.persistence.EntityTransaction)30 EJBException (javax.ejb.EJBException)22 Map (java.util.Map)17 AssertionFailedError (junit.framework.AssertionFailedError)17 TestFailureException (org.apache.openejb.test.TestFailureException)17 ArrayList (java.util.ArrayList)15 JMSException (javax.jms.JMSException)15 KieSession (org.kie.api.runtime.KieSession)14 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)14 Properties (java.util.Properties)13 InitialContext (javax.naming.InitialContext)13 List (java.util.List)12 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)12 RemoteException (java.rmi.RemoteException)11 Query (javax.persistence.Query)11 PrintWriter (java.io.PrintWriter)10