Search in sources :

Example 1 with MyDynamicEntity

use of org.eclipse.persistence.testing.models.jpa.xml.advanced.dynamic.MyDynamicEntity in project eclipselink by eclipse-ee4j.

the class EntityMappingsDynamicAdvancedJUnitTestCase method testDynamicWithNoPersistenceXML.

/**
 * Test a dynamic persistence unit using no persistence.xml.
 */
public void testDynamicWithNoPersistenceXML() {
    // This is a Java SE feature only.
    if (!isOnServer()) {
        List<ClassDescriptor> descriptors = new ArrayList<ClassDescriptor>();
        RelationalDescriptor descriptor = new RelationalDescriptor();
        descriptor.setJavaClassName("org.eclipse.persistence.testing.models.jpa.xml.advanced.dynamic.MyDynamicEntity");
        descriptor.setAlias("MyDynamicEntity");
        descriptor.setTableName("JPA_DYNAMIC_ENTITY");
        descriptor.addPrimaryKeyFieldName("ID");
        descriptor.setSequenceNumberFieldName("ID");
        descriptor.setSequenceNumberName("DYNAMIC_SEQ");
        descriptor.addDirectMapping("id", "ID");
        descriptor.addDirectMapping("firstName", "F_NAME");
        descriptor.addDirectMapping("lastName", "L_NAME");
        descriptors.add(descriptor);
        EntityManager em = createDynamicEntityManager("dynamic-test", descriptors);
        new DynamicTableCreator().replaceTables(((JpaEntityManager) em).getServerSession());
        try {
            beginTransaction(em);
            em.persist(new MyDynamicEntity("Doug", "Clarke"));
            em.persist(new MyDynamicEntity("Peter", "Krogh"));
            commitTransaction(em);
            clearCache("dynamic-test");
            em.clear();
            List<MyDynamicEntity> results = em.createQuery("SELECT d FROM MyDynamicEntity d").getResultList();
            assertFalse("No dynamic entities were returned from the query", results.isEmpty());
        } catch (RuntimeException e) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            throw e;
        } finally {
            closeEntityManager(em);
        }
    }
}
Also used : RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) ArrayList(java.util.ArrayList) MyDynamicEntity(org.eclipse.persistence.testing.models.jpa.xml.advanced.dynamic.MyDynamicEntity) AdvancedDynamicTableCreator(org.eclipse.persistence.testing.models.jpa.xml.advanced.dynamic.AdvancedDynamicTableCreator) DynamicTableCreator(org.eclipse.persistence.testing.models.jpa.xml.advanced.dynamic.DynamicTableCreator)

Aggregations

EntityManager (jakarta.persistence.EntityManager)1 ArrayList (java.util.ArrayList)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)1 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)1 AdvancedDynamicTableCreator (org.eclipse.persistence.testing.models.jpa.xml.advanced.dynamic.AdvancedDynamicTableCreator)1 DynamicTableCreator (org.eclipse.persistence.testing.models.jpa.xml.advanced.dynamic.DynamicTableCreator)1 MyDynamicEntity (org.eclipse.persistence.testing.models.jpa.xml.advanced.dynamic.MyDynamicEntity)1