Search in sources :

Example 26 with DynamicType

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

the class SimpleTypes_OneToOne method createSimpleAwithSimpleB.

@Test
public void createSimpleAwithSimpleB() {
    DynamicType simpleTypeA = dynamicHelper.getType("SimpleA");
    Assert.assertNotNull(simpleTypeA);
    DynamicType simpleTypeB = dynamicHelper.getType("SimpleB");
    Assert.assertNotNull(simpleTypeB);
    Assert.assertNotNull(session.getDescriptorForAlias("SimpleB"));
    DynamicEntity simpleInstanceB = simpleTypeB.newDynamicEntity();
    simpleInstanceB.set("id", 2);
    simpleInstanceB.set("value1", "B2");
    DynamicEntity simpleInstanceA = simpleTypeA.newDynamicEntity();
    simpleInstanceA.set("id", 2);
    simpleInstanceA.set("value1", "A2");
    simpleInstanceA.set("b", simpleInstanceB);
    UnitOfWork uow = session.acquireUnitOfWork();
    uow.registerNewObject(simpleInstanceA);
    uow.registerNewObject(simpleInstanceB);
    uow.commit();
    assertEquals(2, qTracker.getTotalSQLINSERTCalls());
    // There is no reason for a shallow insert and an update in this mapping
    assertEquals("No update expected for new objects with 1:1", 0, qTracker.getTotalSQLUPDATECalls());
    ReportQuery countQuery = dynamicHelper.newReportQuery("SimpleB", new ExpressionBuilder());
    countQuery.addCount();
    countQuery.setShouldReturnSingleValue(true);
    Assert.assertEquals(1, ((Number) session.executeQuery(countQuery)).intValue());
    countQuery = dynamicHelper.newReportQuery("SimpleA", new ExpressionBuilder());
    countQuery.addCount();
    countQuery.setShouldReturnSingleValue(true);
    Assert.assertEquals(1, ((Number) session.executeQuery(countQuery)).intValue());
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) ReportQuery(org.eclipse.persistence.queries.ReportQuery) DynamicType(org.eclipse.persistence.dynamic.DynamicType) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Test(org.junit.Test)

Example 27 with DynamicType

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

the class EmployeeProjectTestSuite method readAll.

@SuppressWarnings("unchecked")
@Test
public void readAll() throws Exception {
    createNewInstance();
    DynamicType type = dynamicHelper.getType("Employee");
    List<DynamicEntity> allObjects = session.readAllObjects(type.getJavaClass());
    assertEquals(1, allObjects.size());
}
Also used : DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) DynamicType(org.eclipse.persistence.dynamic.DynamicType) Test(org.junit.Test)

Example 28 with DynamicType

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

the class DynamicHelperTestSuite method setUp.

@BeforeClass
public static void setUp() {
    session = createSession();
    dynamicHelper = new DynamicHelper(session);
    DynamicClassLoader dcl = dynamicHelper.getDynamicClassLoader();
    Class<?> empClass = dcl.createDynamicClass(EMPLOYEE_CLASSNAME);
    DynamicTypeBuilder typeBuilder = new DynamicTypeBuilder(empClass, null, "D_EMPLOYEE");
    typeBuilder.setPrimaryKeyFields("EMP_ID");
    typeBuilder.addDirectMapping("id", int.class, "EMP_ID");
    typeBuilder.addDirectMapping("firstName", String.class, "F_NAME");
    typeBuilder.addDirectMapping("lastName", String.class, "L_NAME");
    dynamicHelper.addTypes(true, false, typeBuilder.getType());
    DynamicType empType = new DynamicHelper(session).getType("Employee");
    assertNotNull("No type found for Employee", empType);
    // Populate table with a single Employee
    DynamicEntity e1 = empType.newDynamicEntity();
    e1.set("id", 1);
    e1.set("firstName", "Mike");
    e1.set("lastName", "Norman");
    UnitOfWork uow = session.acquireUnitOfWork();
    uow.registerNewObject(e1);
    uow.commit();
    session.getIdentityMapAccessor().initializeAllIdentityMaps();
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) DynamicType(org.eclipse.persistence.dynamic.DynamicType) DynamicHelper(org.eclipse.persistence.dynamic.DynamicHelper) DynamicTypeBuilder(org.eclipse.persistence.dynamic.DynamicTypeBuilder) BeforeClass(org.junit.BeforeClass)

Example 29 with DynamicType

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

the class EntityTypeFromScratch method entityTypeFromDescriptor.

@Test
public void entityTypeFromDescriptor() throws Exception {
    DynamicType entityType = buildMyEntityType();
    assertEquals(MyEntity.class, entityType.getJavaClass());
    DatabaseSession session = createSession();
    session.login();
    session.addDescriptor(entityType.getDescriptor());
    new SchemaManager(session).replaceDefaultTables();
    DynamicEntity entity = entityType.newDynamicEntity();
    entity.set("id", 1);
    entity.set("name", "Name");
    session.insertObject(entity);
    session.logout();
}
Also used : DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) DynamicType(org.eclipse.persistence.dynamic.DynamicType) SchemaManager(org.eclipse.persistence.tools.schemaframework.SchemaManager) Test(org.junit.Test)

Example 30 with DynamicType

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

the class SimpleTypes_ManyToMany method verifyConfig.

@Test
public void verifyConfig() throws Exception {
    ClassDescriptor descriptorA = helper.getSession().getClassDescriptorForAlias("SimpleA");
    assertNotNull("No descriptor found for alias='SimpleA'", descriptorA);
    DynamicType simpleTypeA = helper.getType("SimpleA");
    assertNotNull("'SimpleA' EntityType not found", simpleTypeA);
    assertEquals(descriptorA, simpleTypeA.getDescriptor());
    DirectToFieldMapping a_id = (DirectToFieldMapping) descriptorA.getMappingForAttributeName("id");
    assertEquals(int.class, a_id.getAttributeClassification());
    DirectToFieldMapping a_value1 = (DirectToFieldMapping) descriptorA.getMappingForAttributeName("value1");
    assertEquals(String.class, a_value1.getAttributeClassification());
    ClassDescriptor descriptorB = helper.getSession().getClassDescriptorForAlias("SimpleB");
    assertNotNull("No descriptor found for alias='SimpleB'", descriptorB);
    DynamicType simpleTypeB = helper.getType("SimpleB");
    assertNotNull("'SimpleB' EntityType not found", simpleTypeB);
    assertEquals(descriptorB, simpleTypeB.getDescriptor());
    DirectToFieldMapping b_id = (DirectToFieldMapping) descriptorB.getMappingForAttributeName("id");
    assertEquals(int.class, b_id.getAttributeClassification());
    DirectToFieldMapping b_value1 = (DirectToFieldMapping) descriptorB.getMappingForAttributeName("value1");
    assertEquals(String.class, b_value1.getAttributeClassification());
    ManyToManyMapping a_b = (ManyToManyMapping) descriptorA.getMappingForAttributeName("b");
    assertEquals(descriptorB, a_b.getReferenceDescriptor());
}
Also used : DirectToFieldMapping(org.eclipse.persistence.mappings.DirectToFieldMapping) ManyToManyMapping(org.eclipse.persistence.mappings.ManyToManyMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DynamicType(org.eclipse.persistence.dynamic.DynamicType) Test(org.junit.Test)

Aggregations

DynamicType (org.eclipse.persistence.dynamic.DynamicType)76 Test (org.junit.Test)57 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)56 EntityManager (jakarta.persistence.EntityManager)20 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)19 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)15 ReportQuery (org.eclipse.persistence.queries.ReportQuery)15 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)13 JPADynamicHelper (org.eclipse.persistence.jpa.dynamic.JPADynamicHelper)8 DirectToFieldMapping (org.eclipse.persistence.mappings.DirectToFieldMapping)8 HashMap (java.util.HashMap)7 DynamicClassLoader (org.eclipse.persistence.dynamic.DynamicClassLoader)5 OneToManyMapping (org.eclipse.persistence.mappings.OneToManyMapping)4 BufferedReader (java.io.BufferedReader)3 IOException (java.io.IOException)3 InputStreamReader (java.io.InputStreamReader)3 DynamicTypeBuilder (org.eclipse.persistence.dynamic.DynamicTypeBuilder)3 DynamicEntityImpl (org.eclipse.persistence.internal.dynamic.DynamicEntityImpl)3 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)3 DynamicHelper (org.eclipse.persistence.dynamic.DynamicHelper)2