Search in sources :

Example 1 with EqualsBuilder

use of org.apache.cayenne.util.EqualsBuilder in project cayenne by apache.

the class ClientServerChannelIT method testPerformQueryValuePropagation.

@Test
public void testPerformQueryValuePropagation() throws Exception {
    byte[] bytes = new byte[] { 1, 2, 3 };
    tMtTable3.insert(1, bytes, "abc", 4);
    Query query = new SelectQuery("MtTable3");
    QueryResponse response = clientServerChannel.onQuery(null, query);
    assertNotNull(response);
    List<?> results = response.firstList();
    assertNotNull(results);
    assertEquals(1, results.size());
    Object result = results.get(0);
    assertTrue("Result is of wrong type: " + result, result instanceof ClientMtTable3);
    ClientMtTable3 clientObject = (ClientMtTable3) result;
    assertEquals("abc", clientObject.getCharColumn());
    assertEquals(new Integer(4), clientObject.getIntColumn());
    assertTrue(new EqualsBuilder().append(clientObject.getBinaryColumn(), bytes).isEquals());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable3(org.apache.cayenne.testdo.mt.ClientMtTable3) SelectQuery(org.apache.cayenne.query.SelectQuery) MockQuery(org.apache.cayenne.query.MockQuery) Query(org.apache.cayenne.query.Query) QueryResponse(org.apache.cayenne.QueryResponse) EqualsBuilder(org.apache.cayenne.util.EqualsBuilder) Test(org.junit.Test)

Example 2 with EqualsBuilder

use of org.apache.cayenne.util.EqualsBuilder in project cayenne by apache.

the class RelationshipLoader method load.

@Override
public void load(DatabaseMetaData metaData, DbLoadDataStore map) throws SQLException {
    if (config.isSkipRelationshipsLoading()) {
        return;
    }
    for (Map.Entry<String, Set<ExportedKey>> entry : map.getExportedKeysEntrySet()) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Process keys for: " + entry.getKey());
        }
        Set<ExportedKey> exportedKeys = entry.getValue();
        ExportedKey key = exportedKeys.iterator().next();
        if (key == null) {
            throw new IllegalStateException();
        }
        ExportedKey.KeyData PK = key.getPk();
        ExportedKey.KeyData FK = key.getFk();
        DbEntity pkEntity = map.getDbEntity(PK.getTable());
        DbEntity fkEntity = map.getDbEntity(FK.getTable());
        if (pkEntity == null || fkEntity == null) {
            // Check for existence of this entities were made in creation of ExportedKey
            throw new IllegalStateException();
        }
        if (!new EqualsBuilder().append(pkEntity.getCatalog(), PK.getCatalog()).append(pkEntity.getSchema(), PK.getSchema()).append(fkEntity.getCatalog(), FK.getCatalog()).append(fkEntity.getSchema(), PK.getSchema()).isEquals()) {
            LOGGER.info("Skip relation: '" + key + "' because it related to objects from other catalog/schema");
            LOGGER.info("     relation primary key: '" + PK.getCatalog() + "." + PK.getSchema() + "'");
            LOGGER.info("       primary key entity: '" + pkEntity.getCatalog() + "." + pkEntity.getSchema() + "'");
            LOGGER.info("     relation foreign key: '" + FK.getCatalog() + "." + FK.getSchema() + "'");
            LOGGER.info("       foreign key entity: '" + fkEntity.getCatalog() + "." + fkEntity.getSchema() + "'");
            continue;
        }
        // forwardRelationship is a reference from table with primary key
        // it is what exactly we load from db
        DbRelationship forwardRelationship = new DbRelationship();
        forwardRelationship.setSourceEntity(pkEntity);
        forwardRelationship.setTargetEntityName(fkEntity);
        // TODO: dirty and non-transparent... using DbRelationshipDetected for the benefit of the merge package.
        // This info is available from joins....
        DbRelationshipDetected reverseRelationship = new DbRelationshipDetected();
        reverseRelationship.setFkName(FK.getName());
        reverseRelationship.setSourceEntity(fkEntity);
        reverseRelationship.setTargetEntityName(pkEntity);
        reverseRelationship.setToMany(false);
        createAndAppendJoins(exportedKeys, pkEntity, fkEntity, forwardRelationship, reverseRelationship);
        boolean toDependentPK = isToDependentPK(forwardRelationship);
        boolean toMany = isToMany(toDependentPK, fkEntity, forwardRelationship);
        forwardRelationship.setToDependentPK(toDependentPK);
        forwardRelationship.setToMany(toMany);
        // set relationship names only after their joins are ready ...
        // generator logic is based on relationship state...
        setRelationshipName(pkEntity, forwardRelationship);
        setRelationshipName(fkEntity, reverseRelationship);
        checkAndAddRelationship(pkEntity, forwardRelationship);
        checkAndAddRelationship(fkEntity, reverseRelationship);
    }
}
Also used : Set(java.util.Set) EqualsBuilder(org.apache.cayenne.util.EqualsBuilder) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) Map(java.util.Map)

Aggregations

EqualsBuilder (org.apache.cayenne.util.EqualsBuilder)2 Map (java.util.Map)1 Set (java.util.Set)1 QueryResponse (org.apache.cayenne.QueryResponse)1 DbEntity (org.apache.cayenne.map.DbEntity)1 DbRelationship (org.apache.cayenne.map.DbRelationship)1 MockQuery (org.apache.cayenne.query.MockQuery)1 Query (org.apache.cayenne.query.Query)1 SelectQuery (org.apache.cayenne.query.SelectQuery)1 ClientMtTable3 (org.apache.cayenne.testdo.mt.ClientMtTable3)1 Test (org.junit.Test)1