Search in sources :

Example 81 with EntityResolver

use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.

the class CayenneContextIT method testDeleteObject.

@Test
public void testDeleteObject() {
    CayenneContext context = new CayenneContext(new MockDataChannel());
    ObjEntity entity = new ObjEntity("test_entity");
    entity.setClassName(MockPersistentObject.class.getName());
    DataMap dataMap = new DataMap("test");
    dataMap.addObjEntity(entity);
    Collection<DataMap> entities = Collections.singleton(dataMap);
    context.setEntityResolver(new EntityResolver(entities));
    // TRANSIENT ... should quietly ignore it
    Persistent transientObject = new MockPersistentObject();
    context.deleteObjects(transientObject);
    assertEquals(PersistenceState.TRANSIENT, transientObject.getPersistenceState());
    // NEW ... should make it TRANSIENT
    // create via context to make sure that object store would register it
    Persistent newObject = context.newObject(MockPersistentObject.class);
    assertNotNull(newObject.getObjectContext());
    context.deleteObjects(newObject);
    assertNull(newObject.getObjectContext());
    assertEquals(PersistenceState.TRANSIENT, newObject.getPersistenceState());
    assertFalse(context.internalGraphManager().dirtyNodes().contains(newObject.getObjectId()));
    // see CAY-547 for details...
    assertFalse(context.internalGraphManager().dirtyNodes().contains(null));
    // COMMITTED
    Persistent committed = new MockPersistentObject();
    committed.setPersistenceState(PersistenceState.COMMITTED);
    committed.setObjectId(ObjectId.of("test_entity", "key", "value1"));
    committed.setObjectContext(context);
    context.deleteObjects(committed);
    assertEquals(PersistenceState.DELETED, committed.getPersistenceState());
    // MODIFIED
    Persistent modified = new MockPersistentObject();
    modified.setPersistenceState(PersistenceState.MODIFIED);
    modified.setObjectId(ObjectId.of("test_entity", "key", "value2"));
    modified.setObjectContext(context);
    context.deleteObjects(modified);
    assertEquals(PersistenceState.DELETED, modified.getPersistenceState());
    // DELETED
    Persistent deleted = new MockPersistentObject();
    deleted.setPersistenceState(PersistenceState.DELETED);
    deleted.setObjectId(ObjectId.of("test_entity", "key", "value3"));
    deleted.setObjectContext(context);
    context.deleteObjects(deleted);
    assertEquals(PersistenceState.DELETED, committed.getPersistenceState());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) EntityResolver(org.apache.cayenne.map.EntityResolver) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 82 with EntityResolver

use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.

the class CayenneContextIT method testNewObject.

@Test
public void testNewObject() {
    CayenneContext context = new CayenneContext(new MockDataChannel());
    ObjEntity entity = new ObjEntity("test_entity");
    entity.setClassName(MockPersistentObject.class.getName());
    DataMap dataMap = new DataMap("test");
    dataMap.addObjEntity(entity);
    Collection<DataMap> entities = Collections.singleton(dataMap);
    context.setEntityResolver(new EntityResolver(entities));
    Persistent object = context.newObject(MockPersistentObject.class);
    assertNotNull(object);
    assertTrue(object instanceof MockPersistentObject);
    assertEquals(PersistenceState.NEW, object.getPersistenceState());
    assertSame(context, object.getObjectContext());
    assertTrue(context.internalGraphManager().dirtyNodes(PersistenceState.NEW).contains(object));
    assertNotNull(object.getObjectId());
    assertTrue(object.getObjectId().isTemporary());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) EntityResolver(org.apache.cayenne.map.EntityResolver) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 83 with EntityResolver

use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.

the class ClientServerChannelIT method testGetEntityResolver.

@Test
public void testGetEntityResolver() {
    EntityResolver resolver = clientServerChannel.getEntityResolver();
    assertNotNull(resolver);
    assertNull(resolver.getObjEntity(ClientMtTable1.class));
    assertNotNull(resolver.getClientEntityResolver().getObjEntity(ClientMtTable1.class));
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) EntityResolver(org.apache.cayenne.map.EntityResolver) Test(org.junit.Test)

Example 84 with EntityResolver

use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.

the class ObjectContextChangeLogTest method testGetDiffsSerializableWithHessian.

@Test
public void testGetDiffsSerializableWithHessian() throws Exception {
    ObjectContextChangeLog recorder = new ObjectContextChangeLog();
    // id must be a serializable object
    recorder.addOperation(new NodeCreateOperation("id-string"));
    CompoundDiff diff = (CompoundDiff) recorder.getDiffs();
    Object clone = HessianUtil.cloneViaClientServerSerialization(diff, new EntityResolver());
    assertNotNull(clone);
    assertTrue(clone instanceof CompoundDiff);
    CompoundDiff d1 = (CompoundDiff) clone;
    assertEquals(1, d1.getDiffs().size());
}
Also used : NodeCreateOperation(org.apache.cayenne.graph.NodeCreateOperation) EntityResolver(org.apache.cayenne.map.EntityResolver) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 85 with EntityResolver

use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.

the class DbGenerator method prepareDbEntities.

/**
 * Helper method that orders DbEntities to satisfy referential constraints
 * and returns an ordered list. It also filters out DerivedDbEntities.
 */
private void prepareDbEntities(Collection<DbEntity> excludedEntities) {
    if (excludedEntities == null) {
        excludedEntities = Collections.emptyList();
    }
    List<DbEntity> tables = new ArrayList<>();
    List<DbEntity> tablesWithAutoPk = new ArrayList<>();
    for (DbEntity nextEntity : map.getDbEntities()) {
        // tables with no columns are not included
        if (nextEntity.getAttributes().size() == 0) {
            logObj.info("Skipping entity with no attributes: " + nextEntity.getName());
            continue;
        }
        // check if this entity is explicitly excluded
        if (excludedEntities.contains(nextEntity)) {
            continue;
        }
        // tables with invalid DbAttributes are not included
        boolean invalidAttributes = false;
        for (final DbAttribute attr : nextEntity.getAttributes()) {
            if (attr.getType() == TypesMapping.NOT_DEFINED) {
                logObj.info("Skipping entity, attribute type is undefined: " + nextEntity.getName() + "." + attr.getName());
                invalidAttributes = true;
                break;
            }
        }
        if (invalidAttributes) {
            continue;
        }
        tables.add(nextEntity);
        // check if an automatic PK generation can be potentially supported
        // in this entity. For now simply check that the key is not
        // propagated
        Iterator<DbRelationship> relationships = nextEntity.getRelationships().iterator();
        // create a copy of the original PK list,
        // since the list will be modified locally
        List<DbAttribute> pkAttributes = new ArrayList<>(nextEntity.getPrimaryKeys());
        while (pkAttributes.size() > 0 && relationships.hasNext()) {
            DbRelationship nextRelationship = relationships.next();
            if (!nextRelationship.isToMasterPK()) {
                continue;
            }
            // so
            for (DbJoin join : nextRelationship.getJoins()) {
                pkAttributes.remove(join.getSource());
            }
        }
        // is not propagated via relationship
        if (pkAttributes.size() > 0) {
            tablesWithAutoPk.add(nextEntity);
        }
    }
    // sort table list
    if (tables.size() > 1) {
        EntitySorter sorter = new AshwoodEntitySorter();
        sorter.setEntityResolver(new EntityResolver(Collections.singleton(map)));
        sorter.sortDbEntities(tables, false);
    }
    this.dbEntitiesInInsertOrder = tables;
    this.dbEntitiesRequiringAutoPK = tablesWithAutoPk;
}
Also used : EntitySorter(org.apache.cayenne.map.EntitySorter) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) DbEntity(org.apache.cayenne.map.DbEntity) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) DbRelationship(org.apache.cayenne.map.DbRelationship) ArrayList(java.util.ArrayList) DbAttribute(org.apache.cayenne.map.DbAttribute) DbJoin(org.apache.cayenne.map.DbJoin) EntityResolver(org.apache.cayenne.map.EntityResolver)

Aggregations

EntityResolver (org.apache.cayenne.map.EntityResolver)87 Test (org.junit.Test)57 ObjEntity (org.apache.cayenne.map.ObjEntity)17 ObjectId (org.apache.cayenne.ObjectId)14 DataMap (org.apache.cayenne.map.DataMap)14 DbEntity (org.apache.cayenne.map.DbEntity)10 ArrayList (java.util.ArrayList)7 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)7 HashMap (java.util.HashMap)6 QueryResponse (org.apache.cayenne.QueryResponse)6 DataNode (org.apache.cayenne.access.DataNode)6 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)6 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)6 MockEventManager (org.apache.cayenne.event.MockEventManager)5 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)5 GenericResponse (org.apache.cayenne.util.GenericResponse)5 List (java.util.List)4 MappingNamespace (org.apache.cayenne.map.MappingNamespace)4 Artist (org.apache.cayenne.testdo.testmap.Artist)4 Map (java.util.Map)3