Search in sources :

Example 1 with PhoneNumber

use of org.eclipse.persistence.testing.models.jpa.extensibility.PhoneNumber in project eclipselink by eclipse-ee4j.

the class ExtensibilityTests method testMergeRefreshedManyToMany.

public void testMergeRefreshedManyToMany() {
    EntityManagerFactory emf = getEntityManagerFactory();
    persistEmployeeData(emf);
    EntityManager em = emf.createEntityManager();
    Employee emp = null;
    PhoneNumber pn = null;
    try {
        beginTransaction(em);
        emp = (Employee) em.createQuery("select e from ExtensibilityEmployee e where e.firstName = 'Joe'").getSingleResult();
        pn = new PhoneNumber();
        pn.setAreaCode("613");
        pn.setNumber("1111111");
        em.persist(pn);
        List numbers = new ArrayList();
        numbers.add(pn);
        emp.putExt("phoneNumbers", numbers);
        commitTransaction(em);
        Map properties = new HashMap();
        properties.put(PersistenceUnitProperties.METADATA_SOURCE_XML_FILE, "extension2.xml");
        JpaHelper.getEntityManagerFactory(em).refreshMetadata(properties);
        em.close();
        em = emf.createEntityManager();
        emp = em.merge(emp);
        assertNull(emp.getExt("phoneNumbers"));
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        em.close();
        deleteEmployeeData(emf);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.extensibility.Employee) HashMap(java.util.HashMap) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) ArrayList(java.util.ArrayList) PhoneNumber(org.eclipse.persistence.testing.models.jpa.extensibility.PhoneNumber) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with PhoneNumber

use of org.eclipse.persistence.testing.models.jpa.extensibility.PhoneNumber in project eclipselink by eclipse-ee4j.

the class ExtensibilityTests method testOneToManyMapping.

public void testOneToManyMapping() {
    EntityManagerFactory emf = getEntityManagerFactory();
    persistEmployeeData(emf);
    EntityManager em = emf.createEntityManager();
    PhoneNumber pn = new PhoneNumber();
    Employee emp = null;
    try {
        beginTransaction(em);
        emp = (Employee) em.createQuery("select e from ExtensibilityEmployee e where e.firstName = 'Joe'").getSingleResult();
        pn.setAreaCode("613");
        pn.setNumber("1111111");
        em.persist(pn);
        List numbers = new ArrayList();
        numbers.add(pn);
        emp.putExt("phoneNumbers", numbers);
        commitTransaction(em);
        em.refresh(emp);
        numbers = emp.getExt("phoneNumbers");
        assertTrue("The phoneNumbers were not properly saved", numbers.size() == 1);
        em.clear();
        clearCache();
        emp = (Employee) em.createQuery("select e from ExtensibilityEmployee e join e.phoneNumbers p where p.areaCode = '613'").getSingleResult();
        assertTrue("queries on extended OneToMany mappings fail", emp != null);
        assertTrue("queries on extended OneToMany mappings return incorrect results.", ((List) emp.getExt("phoneNumbers")).size() == 1);
    } finally {
        try {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            em.close();
            deleteEmployeeData(emf);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.extensibility.Employee) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) ArrayList(java.util.ArrayList) PhoneNumber(org.eclipse.persistence.testing.models.jpa.extensibility.PhoneNumber) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

EntityManager (jakarta.persistence.EntityManager)2 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)2 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)2 Employee (org.eclipse.persistence.testing.models.jpa.extensibility.Employee)2 PhoneNumber (org.eclipse.persistence.testing.models.jpa.extensibility.PhoneNumber)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1