Search in sources :

Example 26 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class SimpleTypeCompositeKeyTestSuite method setUp.

@BeforeClass
public static void setUp() throws Exception {
    emf = DynamicTestHelper.createEMF(DYNAMIC_PERSISTENCE_NAME);
    helper = new JPADynamicHelper(emf);
    DynamicClassLoader dcl = helper.getDynamicClassLoader();
    Class<?> javaType = dcl.createDynamicClass("model.Simple");
    DynamicTypeBuilder typeBuilder = new JPADynamicTypeBuilder(javaType, null, "SIMPLE_TYPE");
    typeBuilder.setPrimaryKeyFields("SID1", "SID2");
    typeBuilder.addDirectMapping("id1", int.class, "SID1");
    typeBuilder.addDirectMapping("id2", int.class, "SID2");
    typeBuilder.addDirectMapping("value1", String.class, "VAL_1");
    typeBuilder.addDirectMapping("value2", boolean.class, "VAL_2");
    typeBuilder.addDirectMapping("value3", Calendar.class, "VAL_3");
    typeBuilder.addDirectMapping("value4", Character.class, "VAL_4");
    helper.addTypes(true, true, typeBuilder.getType());
    simpleType = helper.getType("Simple");
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) JPADynamicTypeBuilder(org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder) JPADynamicTypeBuilder(org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder) DynamicTypeBuilder(org.eclipse.persistence.dynamic.DynamicTypeBuilder) JPADynamicHelper(org.eclipse.persistence.jpa.dynamic.JPADynamicHelper) BeforeClass(org.junit.BeforeClass)

Example 27 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class SimpleTypes_AggregateObject method setUp.

@BeforeClass
public static void setUp() {
    emf = DynamicTestHelper.createEMF(DYNAMIC_PERSISTENCE_NAME);
    helper = new JPADynamicHelper(emf);
    DynamicClassLoader dcl = helper.getDynamicClassLoader();
    Class<?> simpleTypeB = dcl.createDynamicClass("model.SimpleB");
    JPADynamicTypeBuilder bFactory = new JPADynamicTypeBuilder(simpleTypeB, null);
    bFactory.addDirectMapping("value2", boolean.class, "VAL_2");
    bFactory.addDirectMapping("value3", String.class, "VAL_3");
    Class<?> simpleTypeC = dcl.createDynamicClass("model.SimpleC");
    JPADynamicTypeBuilder cFactory = new JPADynamicTypeBuilder(simpleTypeC, null);
    cFactory.addDirectMapping("value4", double.class, "VAL_4");
    cFactory.addDirectMapping("value5", String.class, "VAL_5");
    Class<?> simpleTypeA = dcl.createDynamicClass("model.SimpleA");
    JPADynamicTypeBuilder aFactory = new JPADynamicTypeBuilder(simpleTypeA, null, "SIMPLE_TYPE_A");
    aFactory.setPrimaryKeyFields("SID");
    aFactory.addDirectMapping("id", int.class, "SID");
    aFactory.addDirectMapping("value1", String.class, "VAL_1");
    aFactory.addAggregateObjectMapping("b", bFactory.getType(), true);
    aFactory.addAggregateObjectMapping("c", cFactory.getType(), false);
    helper.addTypes(true, true, aFactory.getType(), bFactory.getType(), cFactory.getType());
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) JPADynamicTypeBuilder(org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder) JPADynamicHelper(org.eclipse.persistence.jpa.dynamic.JPADynamicHelper) BeforeClass(org.junit.BeforeClass)

Example 28 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class DefaultSequencingTestSuite method setUp.

@BeforeClass
public static void setUp() {
    emf = DynamicTestHelper.createEMF(DYNAMIC_PERSISTENCE_NAME);
    helper = new JPADynamicHelper(emf);
    DynamicClassLoader dcl = helper.getDynamicClassLoader();
    Class<?> javaType = dcl.createDynamicClass("model.sequencing." + ENTITY_TYPE);
    DynamicTypeBuilder typeBuilder = new JPADynamicTypeBuilder(javaType, null, TABLE_NAME);
    typeBuilder.setPrimaryKeyFields("SID");
    typeBuilder.addDirectMapping("id", int.class, "SID");
    typeBuilder.addDirectMapping("value1", String.class, "VAL_1");
    TableSequence defaultSequence = (TableSequence) helper.getSession().getLogin().getDefaultSequence();
    defaultSequence.setTableName(SEQ_TABLE_NAME);
    typeBuilder.configureSequencing(ENTITY_TYPE + "_SEQ", "SID");
    helper.addTypes(true, true, typeBuilder.getType());
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) JPADynamicTypeBuilder(org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder) JPADynamicTypeBuilder(org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder) DynamicTypeBuilder(org.eclipse.persistence.dynamic.DynamicTypeBuilder) JPADynamicHelper(org.eclipse.persistence.jpa.dynamic.JPADynamicHelper) TableSequence(org.eclipse.persistence.sequencing.TableSequence) BeforeClass(org.junit.BeforeClass)

Example 29 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class EntityMappingsDynamicAdvancedJUnitTestCase method getDynamicProperties.

/**
 * Return the properties needed for dynamic persistence.
 */
protected Map getDynamicProperties() {
    Map properties = JUnitTestCaseHelper.getDatabaseProperties();
    properties.put(PersistenceUnitProperties.CLASSLOADER, new DynamicClassLoader(Thread.currentThread().getContextClassLoader()));
    return properties;
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class BeanValidationDynamicEntityJunitTest method testPersistDynamicEntityWithInvalidData.

/**
 * Strategy:
 * 1. Persist a dynamic entity with invalid values
 * 2. Assert - a ConstraintViolationException is raised
 * 3. Assert - transaction is rolled back
 * 4. Assert - The validation exception is due to invalid value given by us.
 */
@SuppressWarnings("unchecked")
public void testPersistDynamicEntityWithInvalidData() {
    // Create an entity manager factory with a dynamic class loader.
    DynamicClassLoader dcl = new ReflectiveDynamicClassLoader(Thread.currentThread().getContextClassLoader());
    Map<String, Object> properties = new HashMap<>(getPersistenceProperties());
    properties.put(PersistenceUnitProperties.CLASSLOADER, dcl);
    // properties.put(PersistenceUnitProperties.BEAN_VALIDATION_NO_OPTIMISATION, "true");
    EntityManager em = createEntityManager("beanvalidation-dynamic", properties);
    EntityManagerFactory entityManagerFactory = em.getEntityManagerFactory();
    // Create types
    JPADynamicHelper helper = new JPADynamicHelper(entityManagerFactory);
    DynamicType empType = helper.getType("DynamicEmployee");
    ClassDescriptor empTypeDescriptor = empType.getDescriptor();
    DynamicEntity employee = (DynamicEntity) empTypeDescriptor.getInstantiationPolicy().buildNewInstance();
    employee.set("name", "Bob");
    employee.set("surname", "Smith");
    try {
        beginTransaction(em);
        em.persist(employee);
        commitTransaction(em);
    } catch (Exception e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        Assert.fail(e.getMessage());
    }
    Integer employeeId = employee.<Integer>get("id");
    assertTrue("Employee id not set!", employeeId > 0);
    DynamicEntity emp = em.find(empType.getJavaClass(), employeeId);
    emp.set("name", null);
    emp.set("surname", null);
    try {
        beginTransaction(em);
        em.flush();
    } catch (ConstraintViolationException e) {
        System.out.println("ConstraintException: " + e.getMessage());
        for (ConstraintViolation<?> cv : e.getConstraintViolations()) {
            System.out.println("\t>> " + cv.getPropertyPath() + "::" + cv.getMessage());
        }
        return;
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
    Assert.fail("ConstraintViolationException not thrown");
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) ReflectiveDynamicClassLoader(org.eclipse.persistence.internal.jpa.config.metadata.ReflectiveDynamicClassLoader) ReflectiveDynamicClassLoader(org.eclipse.persistence.internal.jpa.config.metadata.ReflectiveDynamicClassLoader) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) HashMap(java.util.HashMap) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) DynamicType(org.eclipse.persistence.dynamic.DynamicType) ConstraintViolationException(jakarta.validation.ConstraintViolationException) EntityManager(jakarta.persistence.EntityManager) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) JPADynamicHelper(org.eclipse.persistence.jpa.dynamic.JPADynamicHelper)

Aggregations

DynamicClassLoader (org.eclipse.persistence.dynamic.DynamicClassLoader)58 BeforeClass (org.junit.BeforeClass)27 DynamicTypeBuilder (org.eclipse.persistence.dynamic.DynamicTypeBuilder)26 DynamicHelper (org.eclipse.persistence.dynamic.DynamicHelper)17 Test (org.junit.Test)17 JPADynamicHelper (org.eclipse.persistence.jpa.dynamic.JPADynamicHelper)15 JPADynamicTypeBuilder (org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder)13 HashMap (java.util.HashMap)7 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)6 DynamicType (org.eclipse.persistence.dynamic.DynamicType)5 IOException (java.io.IOException)4 TableSequence (org.eclipse.persistence.sequencing.TableSequence)4 EntityManager (jakarta.persistence.EntityManager)3 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 Project (org.eclipse.persistence.sessions.Project)3 EclipseLinkClassWriter (org.eclipse.persistence.dynamic.EclipseLinkClassWriter)2 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)2