Search in sources :

Example 6 with DynamicEntityImpl

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

the class SimpleTypes_AggregateObject method verifyChangTracking.

@Test
public void verifyChangTracking() {
    persistSimpleA();
    DynamicTypeImpl simpleTypeA = (DynamicTypeImpl) helper.getType("SimpleA");
    assertNotNull(simpleTypeA);
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    DynamicEntityImpl a = (DynamicEntityImpl) em.find(simpleTypeA.getJavaClass(), 1);
    assertNotNull(a);
    assertNotNull(a._persistence_getPropertyChangeListener());
    DynamicEntityImpl c = a.<DynamicEntityImpl>get("c");
    assertNotNull(c);
    assertNotNull(c._persistence_getPropertyChangeListener());
    assertTrue(c._persistence_getPropertyChangeListener() instanceof AggregateAttributeChangeListener);
    em.getTransaction().rollback();
    em.close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) DynamicEntityImpl(org.eclipse.persistence.internal.dynamic.DynamicEntityImpl) AggregateAttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AggregateAttributeChangeListener) DynamicTypeImpl(org.eclipse.persistence.internal.dynamic.DynamicTypeImpl) Test(org.junit.Test)

Example 7 with DynamicEntityImpl

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

the class DynamicTestCases method compareDynamicObjects.

private void compareDynamicObjects(Object controlObject, Object testObject) throws Exception {
    if (objectsAlreadyCheckedForEquality.contains(testObject)) {
        // To handle cyclic relationships, only check each object once.
        return;
    }
    if (!(controlObject instanceof DynamicEntityImpl)) {
        fail("controlObject [" + controlObject + "] was not an instance of DynamicEntityImpl.");
    }
    if (!(testObject instanceof DynamicEntityImpl)) {
        fail("testObject [" + testObject + "] was not an instance of DynamicEntityImpl.");
    }
    DynamicEntityImpl dynamicControl = (DynamicEntityImpl) controlObject;
    DynamicEntityImpl dynamicTest = (DynamicEntityImpl) testObject;
    objectsAlreadyCheckedForEquality.add(testObject);
    if (dynamicControl.getType().getNumberOfProperties() != dynamicTest.getType().getNumberOfProperties()) {
        fail("testObject and controlObject did not have the same number of properties.");
    }
    List<String> propNames = dynamicControl.getType().getPropertiesNames();
    Iterator<String> it = propNames.iterator();
    while (it.hasNext()) {
        String propName = it.next();
        Object controlValue = getValue(dynamicControl, propName);
        Object testValue = getValue(dynamicTest, propName);
        if ((testValue instanceof DynamicEntityImpl) && (controlValue instanceof DynamicEntityImpl)) {
            compareDynamicObjects(controlValue, testValue);
        } else if ((testValue instanceof List) && (controlValue instanceof List)) {
            if ((((List) testValue).size()) != (((List) controlValue).size())) {
                fail(dynamicControl.getType().getName() + " " + propName + ": testValue was " + testValue + " but controlValue was " + controlValue);
            }
            for (int i = 0; i < ((List) controlValue).size(); i++) {
                compareDynamicObjects(((List) controlValue).get(i), ((List) testValue).get(i));
            }
        } else {
            if (testValue == null && controlValue == null) {
                continue;
            }
            if (testValue == null && controlValue != null) {
                fail(dynamicControl.getType().getName() + " " + propName + ": testValue was " + testValue + " but controlValue was " + controlValue);
            }
            if (testValue != null && controlValue == null) {
                fail(dynamicTest.getType().getName() + " " + propName + ": testValue was " + testValue + " but controlValue was " + controlValue);
            }
            if (!testValue.equals(controlValue)) {
                fail(propName + ": testValue was " + testValue + " but controlValue was " + controlValue);
            }
        }
    }
}
Also used : DynamicEntityImpl(org.eclipse.persistence.internal.dynamic.DynamicEntityImpl) List(java.util.List) ArrayList(java.util.ArrayList)

Example 8 with DynamicEntityImpl

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

the class IdHelper method buildObjectShell.

/**
 * build a shell of an object based on a primary key.  The object shell will be an instance of the object with
 * only primary key populated
 */
public static Object buildObjectShell(PersistenceContext context, String entityType, Object id) {
    ClassDescriptor descriptor = context.getDescriptor(entityType);
    List<DatabaseMapping> pkMappings = descriptor.getObjectBuilder().getPrimaryKeyMappings();
    Object entity = null;
    if (descriptor.hasCMPPolicy()) {
        CMP3Policy policy = (CMP3Policy) descriptor.getCMPPolicy();
        entity = policy.createBeanUsingKey(id, (AbstractSession) context.getServerSession());
    } else if (DynamicEntity.class.isAssignableFrom(descriptor.getJavaClass())) {
        DynamicEntityImpl dynamicEntity = (DynamicEntityImpl) context.newEntity(entityType);
        // represents the value of that mapping
        if (pkMappings.size() == 1) {
            dynamicEntity.set(pkMappings.get(0).getAttributeName(), id);
        } else {
            // If there are more that one PK, we assume an array as produced
            // by buildId() above with the keys
            // based on a sorted order of PK fields
            List<SortableKey> pkIndices = new ArrayList<>();
            int index = 0;
            for (DatabaseMapping mapping : pkMappings) {
                pkIndices.add(new SortableKey(mapping, index));
                index++;
            }
            Collections.sort(pkIndices);
            Object[] keyElements = (Object[]) id;
            for (SortableKey key : pkIndices) {
                dynamicEntity.set(key.getMapping().getAttributeName(), keyElements[key.getIndex()]);
            }
        }
        entity = dynamicEntity;
    } else {
        throw new RuntimeException("Could not create shell for entity.");
    }
    if (entity instanceof PersistenceEntity) {
        ((PersistenceEntity) entity)._persistence_setId(id);
    }
    if (entity instanceof FetchGroupTracker) {
        ((FetchGroupTracker) entity)._persistence_setSession(JpaHelper.getDatabaseSession(context.getEmf()));
    }
    return entity;
}
Also used : CMP3Policy(org.eclipse.persistence.internal.jpa.CMP3Policy) DynamicEntityImpl(org.eclipse.persistence.internal.dynamic.DynamicEntityImpl) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) PersistenceEntity(org.eclipse.persistence.internal.descriptors.PersistenceEntity) FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) ArrayList(java.util.ArrayList) List(java.util.List) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

DynamicEntityImpl (org.eclipse.persistence.internal.dynamic.DynamicEntityImpl)8 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DynamicType (org.eclipse.persistence.dynamic.DynamicType)3 EntityManager (jakarta.persistence.EntityManager)2 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)2 AggregateAttributeChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.AggregateAttributeChangeListener)2 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)2 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)2 Marshaller (jakarta.xml.bind.Marshaller)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 PersistenceEntity (org.eclipse.persistence.internal.descriptors.PersistenceEntity)1 DynamicTypeImpl (org.eclipse.persistence.internal.dynamic.DynamicTypeImpl)1 CMP3Policy (org.eclipse.persistence.internal.jpa.CMP3Policy)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)1 LinkAdapter (org.eclipse.persistence.jpa.rs.util.xmladapters.LinkAdapter)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1