Search in sources :

Example 1 with Employee

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

the class ExtensibilityTests method testSimpleRefresh.

public void testSimpleRefresh() {
    EntityManagerFactory emf = getEntityManagerFactory();
    EntityManager em = emf.createEntityManager();
    WeakReference<EntityManagerFactoryDelegate> emfRef = new WeakReference<EntityManagerFactoryDelegate>(JpaHelper.getEntityManagerFactory(em).unwrap());
    ServerSession session = (ServerSession) getDatabaseSession();
    RelationalDescriptor addDescriptor = (RelationalDescriptor) session.getProject().getDescriptor(Address.class);
    assertTrue(addDescriptor.getMappingForAttributeName("pobox") != null);
    RelationalDescriptor empDescriptor = (RelationalDescriptor) session.getProject().getDescriptor(Employee.class);
    assertTrue(empDescriptor.getMappingForAttributeName("phoneNumbers") != null);
    session = null;
    Map properties = new HashMap();
    properties.put(PersistenceUnitProperties.METADATA_SOURCE_XML_FILE, "extension2.xml");
    JpaHelper.getEntityManagerFactory(em).refreshMetadata(properties);
    session = (ServerSession) getDatabaseSession();
    addDescriptor = (RelationalDescriptor) session.getProject().getDescriptor(Address.class);
    assertTrue(addDescriptor.getMappingForAttributeName("pobox") == null);
    assertTrue(addDescriptor.getMappingForAttributeName("appartmentNumber") != null);
    empDescriptor = (RelationalDescriptor) session.getProject().getDescriptor(Employee.class);
    assertTrue(empDescriptor.getMappingForAttributeName("phoneNumbers") == null);
    persistEmployeeData(emf);
    em = emf.createEntityManager();
    try {
        beginTransaction(em);
        Employee emp = (Employee) em.createQuery("select e from ExtensibilityEmployee e where e.firstName = 'Joe'").getSingleResult();
        emp.getAddress().set("appartmentNumber", "111");
        commitTransaction(em);
        em.clear();
        clearCache();
        emp = em.find(Employee.class, emp.getId());
        assertTrue(emp.getAddress().get("appartmentNumber").equals("111"));
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        em.close();
        deleteEmployeeData(emf);
    }
    System.gc();
    assertTrue(emfRef.get() == null);
}
Also used : RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) EntityManagerFactoryDelegate(org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) Employee(org.eclipse.persistence.testing.models.jpa.extensibility.Employee) Address(org.eclipse.persistence.testing.models.jpa.extensibility.Address) HashMap(java.util.HashMap) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) WeakReference(java.lang.ref.WeakReference) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Employee

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

the class ExtensibilityTests method testUntriggerVHOnDetached.

public void testUntriggerVHOnDetached() {
    EntityManagerFactory emf = getEntityManagerFactory();
    persistEmployeeData(emf);
    EntityManager em = emf.createEntityManager();
    Employee emp = (Employee) em.createQuery("select e from ExtensibilityEmployee e where e.firstName = 'Joe'").getSingleResult();
    try {
        JpaHelper.getEntityManagerFactory(em).refreshMetadata(null);
        assertTrue(emp.getAddress().getCity().equals("Herestowm"));
        em.close();
        em = null;
        emp = null;
    } finally {
        if (em != null && em.isOpen()) {
            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) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) EntityManagerFactory(jakarta.persistence.EntityManagerFactory)

Example 3 with Employee

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

the class ExtensibilityTests method testMergeRefreshed.

public void testMergeRefreshed() {
    EntityManagerFactory emf = getEntityManagerFactory();
    persistEmployeeData(emf);
    EntityManager em = emf.createEntityManager();
    try {
        beginTransaction(em);
        Employee emp = (Employee) em.createQuery("select e from ExtensibilityEmployee e where e.firstName = 'Joe'").getSingleResult();
        Address add = emp.getAddress();
        emp.getAddress().set("appartmentNumber", "112");
        commitTransaction(em);
        Map properties = new HashMap();
        properties.put(PersistenceUnitProperties.METADATA_SOURCE_XML_FILE, "extension.xml");
        JpaHelper.getEntityManagerFactory(em).refreshMetadata(properties);
        em.close();
        em = emf.createEntityManager();
        beginTransaction(em);
        add = em.merge(add);
        add.set("pobox", "111");
        commitTransaction(em);
        em.refresh(add);
        assertTrue(add.get("pobox").equals("111"));
        assertTrue(add.get("appartmentNumber") == null);
    } 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) Address(org.eclipse.persistence.testing.models.jpa.extensibility.Address) HashMap(java.util.HashMap) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Employee

use of org.eclipse.persistence.testing.models.jpa.extensibility.Employee 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 5 with Employee

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

the class ExtensibilityTests method testBasicMapping.

public void testBasicMapping() {
    Map props = new HashMap();
    props.put(PersistenceUnitProperties.SESSION_NAME, "bla1");
    EntityManagerFactory emf = getEntityManagerFactory();
    persistEmployeeData(emf);
    EntityManager em = emf.createEntityManager();
    try {
        beginTransaction(em);
        Employee emp = (Employee) em.createQuery("select e from ExtensibilityEmployee e where e.firstName = 'Joe'").getSingleResult();
        Address add = emp.getAddress();
        add.set("pobox", "111");
        commitTransaction(em);
        em.refresh(emp);
        assertTrue("The pobox was not properly saved", emp.getAddress().get("pobox").equals("111"));
        em.clear();
        clearCache();
        add = (Address) em.createQuery("select a from ExtensibilityAddress a where a.pobox = '111'").getSingleResult();
        assertTrue("queries on extended Basic mappings fail", add != null);
        assertTrue("queries on extended Basic mappings return incorrect results.", add.get("pobox").equals("111"));
    } 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) Address(org.eclipse.persistence.testing.models.jpa.extensibility.Address) HashMap(java.util.HashMap) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

EntityManager (jakarta.persistence.EntityManager)7 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)7 Employee (org.eclipse.persistence.testing.models.jpa.extensibility.Employee)7 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)6 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)6 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Address (org.eclipse.persistence.testing.models.jpa.extensibility.Address)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 PhoneNumber (org.eclipse.persistence.testing.models.jpa.extensibility.PhoneNumber)2 WeakReference (java.lang.ref.WeakReference)1 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)1 EntityManagerFactoryDelegate (org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate)1 ServerSession (org.eclipse.persistence.sessions.server.ServerSession)1