Search in sources :

Example 61 with DbRelationship

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

the class DropRelationshipToModelIT method testForeignKey.

@Test
public void testForeignKey() throws Exception {
    dropTableIfPresent("NEW_TABLE");
    dropTableIfPresent("NEW_TABLE2");
    assertTokensAndExecute(0, 0);
    DbEntity dbEntity1 = new DbEntity("NEW_TABLE");
    DbAttribute e1col1 = new DbAttribute("ID", Types.INTEGER, dbEntity1);
    e1col1.setMandatory(true);
    e1col1.setPrimaryKey(true);
    dbEntity1.addAttribute(e1col1);
    DbAttribute e1col2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity1);
    e1col2.setMaxLength(10);
    e1col2.setMandatory(false);
    dbEntity1.addAttribute(e1col2);
    map.addDbEntity(dbEntity1);
    DbEntity dbEntity2 = new DbEntity("NEW_TABLE2");
    DbAttribute e2col1 = new DbAttribute("ID", Types.INTEGER, dbEntity2);
    e2col1.setMandatory(true);
    e2col1.setPrimaryKey(true);
    dbEntity2.addAttribute(e2col1);
    DbAttribute e2col2 = new DbAttribute("FK", Types.INTEGER, dbEntity2);
    dbEntity2.addAttribute(e2col2);
    DbAttribute e2col3 = new DbAttribute("NAME", Types.VARCHAR, dbEntity2);
    e2col3.setMaxLength(10);
    dbEntity2.addAttribute(e2col3);
    map.addDbEntity(dbEntity2);
    // create db relationships
    DbRelationship rel1To2 = new DbRelationship("rel1To2");
    rel1To2.setSourceEntity(dbEntity1);
    rel1To2.setTargetEntityName(dbEntity2);
    rel1To2.setToMany(true);
    rel1To2.addJoin(new DbJoin(rel1To2, e1col1.getName(), e2col2.getName()));
    dbEntity1.addRelationship(rel1To2);
    DbRelationship rel2To1 = new DbRelationship("rel2To1");
    rel2To1.setSourceEntity(dbEntity2);
    rel2To1.setTargetEntityName(dbEntity1);
    rel2To1.setToMany(false);
    rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
    dbEntity2.addRelationship(rel2To1);
    assertSame(rel1To2, rel2To1.getReverseRelationship());
    assertSame(rel2To1, rel1To2.getReverseRelationship());
    assertTokensAndExecute(3, 0);
    assertTokensAndExecute(0, 0);
    // create ObjEntities
    ObjEntity objEntity1 = new ObjEntity("NewTable");
    objEntity1.setDbEntity(dbEntity1);
    ObjAttribute oatr1 = new ObjAttribute("name");
    oatr1.setDbAttributePath(e1col2.getName());
    oatr1.setType("java.lang.String");
    objEntity1.addAttribute(oatr1);
    map.addObjEntity(objEntity1);
    ObjEntity objEntity2 = new ObjEntity("NewTable2");
    objEntity2.setDbEntity(dbEntity2);
    ObjAttribute o2a1 = new ObjAttribute("name");
    o2a1.setDbAttributePath(e2col3.getName());
    o2a1.setType("java.lang.String");
    objEntity2.addAttribute(o2a1);
    map.addObjEntity(objEntity2);
    // create ObjRelationships
    assertEquals(0, objEntity1.getRelationships().size());
    assertEquals(0, objEntity2.getRelationships().size());
    ObjRelationship objRel1To2 = new ObjRelationship("objRel1To2");
    objRel1To2.addDbRelationship(rel1To2);
    objRel1To2.setSourceEntity(objEntity1);
    objRel1To2.setTargetEntityName(objEntity2);
    objEntity1.addRelationship(objRel1To2);
    ObjRelationship objRel2To1 = new ObjRelationship("objRel2To1");
    objRel2To1.addDbRelationship(rel2To1);
    objRel2To1.setSourceEntity(objEntity2);
    objRel2To1.setTargetEntityName(objEntity1);
    objEntity2.addRelationship(objRel2To1);
    assertEquals(1, objEntity1.getRelationships().size());
    assertEquals(1, objEntity2.getRelationships().size());
    assertSame(objRel1To2, objRel2To1.getReverseRelationship());
    assertSame(objRel2To1, objRel1To2.getReverseRelationship());
    // remove relationship and fk from model, merge to db and read to model
    dbEntity2.removeRelationship(rel2To1.getName());
    dbEntity1.removeRelationship(rel1To2.getName());
    dbEntity2.removeAttribute(e2col2.getName());
    List<MergerToken> tokens = createMergeTokens();
    /**
     * Drop Relationship NEW_TABLE2->NEW_TABLE To DB
     * Drop Column NEW_TABLE2.FK To DB
     */
    assertTokens(tokens, 2, 0);
    for (MergerToken token : tokens) {
        if (token.getDirection().isToDb()) {
            execute(token);
        }
    }
    assertTokensAndExecute(0, 0);
    dbEntity2.addRelationship(rel2To1);
    dbEntity1.addRelationship(rel1To2);
    dbEntity2.addAttribute(e2col2);
    // try do use the merger to remove the relationship in the model
    tokens = createMergeTokens();
    assertTokens(tokens, 2, 0);
    // TODO: reversing the following two tokens should also reverse the
    // order
    MergerToken token0 = tokens.get(0).createReverse(mergerFactory());
    MergerToken token1 = tokens.get(1).createReverse(mergerFactory());
    if (!(token0 instanceof DropRelationshipToModel && token1 instanceof DropColumnToModel || token1 instanceof DropRelationshipToModel && token0 instanceof DropColumnToModel)) {
        fail();
    }
    execute(token0);
    execute(token1);
    // check after merging
    assertNull(dbEntity2.getAttribute(e2col2.getName()));
    assertEquals(0, dbEntity1.getRelationships().size());
    assertEquals(0, dbEntity2.getRelationships().size());
    assertEquals(0, objEntity1.getRelationships().size());
    assertEquals(0, objEntity2.getRelationships().size());
    // clear up
    dbEntity1.removeRelationship(rel1To2.getName());
    dbEntity2.removeRelationship(rel2To1.getName());
    map.removeObjEntity(objEntity1.getName(), true);
    map.removeDbEntity(dbEntity1.getName(), true);
    map.removeObjEntity(objEntity2.getName(), true);
    map.removeDbEntity(dbEntity2.getName(), true);
    resolver.refreshMappingCache();
    assertNull(map.getObjEntity(objEntity1.getName()));
    assertNull(map.getDbEntity(dbEntity1.getName()));
    assertNull(map.getObjEntity(objEntity2.getName()));
    assertNull(map.getDbEntity(dbEntity2.getName()));
    assertFalse(map.getDbEntities().contains(dbEntity1));
    assertFalse(map.getDbEntities().contains(dbEntity2));
    assertTokensAndExecute(2, 0);
    assertTokensAndExecute(0, 0);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DbJoin(org.apache.cayenne.map.DbJoin) Test(org.junit.Test)

Example 62 with DbRelationship

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

the class DefaultObjectNameGeneratorTest method makeRelationship.

private DbRelationship makeRelationship(String srcEntity, String srcKey, String targetEntity, String targetKey, boolean toMany) {
    DbRelationship relationship = new DbRelationship();
    relationship.addJoin(new DbJoin(relationship, srcKey, targetKey));
    relationship.setToMany(toMany);
    relationship.setSourceEntity(new DbEntity(srcEntity));
    relationship.setTargetEntityName(targetEntity);
    return relationship;
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin)

Example 63 with DbRelationship

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

the class NameBuilderTest method testName_DbEntityContext.

@Test
public void testName_DbEntityContext() {
    DbEntity entity = new DbEntity();
    DbAttribute a0 = new DbAttribute();
    String na0 = NameBuilder.builder(a0).in(entity).name();
    assertEquals("untitledAttr", na0);
    a0.setName(na0);
    entity.addAttribute(a0);
    DbAttribute a1 = new DbAttribute();
    String na1 = NameBuilder.builder(a1).in(entity).name();
    assertEquals("untitledAttr1", na1);
    a1.setName(na1);
    entity.addAttribute(a1);
    DbRelationship r0 = new DbRelationship();
    String nr0 = NameBuilder.builder(r0).in(entity).name();
    assertEquals("untitledRel", nr0);
    r0.setName(nr0);
    entity.addRelationship(r0);
    DbRelationship r1 = new DbRelationship();
    String nr1 = NameBuilder.builder(r1).in(entity).name();
    assertEquals("untitledRel1", nr1);
    r1.setName(nr1);
    entity.addRelationship(r1);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 64 with DbRelationship

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

the class DbLoaderIT method testSimpleLoad.

/**
 * Test that parts of loader are in place
 */
@Test
public void testSimpleLoad() throws Exception {
    DbLoader loader = createDbLoader(true, true);
    DataMap loaded = loader.load();
    assertNotNull(loaded);
    assertEquals("__generated_by_dbloader__", loaded.getName());
    // DbEntity
    DbEntity artist = loaded.getDbEntity("ARTIST");
    assertNotNull(artist);
    // DbAttribute
    DbAttribute id = getDbAttribute(artist, "ARTIST_ID");
    assertNotNull(id);
    assertTrue(id.isMandatory());
    assertTrue(id.isPrimaryKey());
    DbAttribute name = getDbAttribute(artist, "ARTIST_NAME");
    assertNotNull(name);
    assertTrue(name.isMandatory());
    DbAttribute date = getDbAttribute(artist, "DATE_OF_BIRTH");
    assertNotNull(date);
    assertFalse(date.isMandatory());
    // DbRelationship
    assertEquals(4, artist.getRelationships().size());
    DbRelationship exhibits = artist.getRelationship("artistExhibits");
    assertNotNull(exhibits);
    assertEquals("ARTIST_EXHIBIT", exhibits.getTargetEntityName().toUpperCase());
    DbEntity target = exhibits.getTargetEntity();
    assertNotNull(target);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 65 with DbRelationship

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

the class RelationshipsLoaderIT method testRelationshipLoad.

@Test
public void testRelationshipLoad() throws Exception {
    boolean supportsFK = accessStackAdapter.supportsFKConstraints();
    if (!supportsFK) {
        return;
    }
    DatabaseMetaData metaData = connection.getMetaData();
    DbLoaderDelegate delegate = new DefaultDbLoaderDelegate();
    // We need all data to check relationships, so simply load it all
    EntityLoader entityLoader = new EntityLoader(adapter, EMPTY_CONFIG, delegate);
    AttributeLoader attributeLoader = new AttributeLoader(adapter, EMPTY_CONFIG, delegate);
    PrimaryKeyLoader primaryKeyLoader = new PrimaryKeyLoader(EMPTY_CONFIG, delegate);
    ExportedKeyLoader exportedKeyLoader = new ExportedKeyLoader(EMPTY_CONFIG, delegate);
    entityLoader.load(metaData, store);
    attributeLoader.load(metaData, store);
    primaryKeyLoader.load(metaData, store);
    exportedKeyLoader.load(metaData, store);
    // *** TESTING THIS ***
    RelationshipLoader relationshipLoader = new RelationshipLoader(EMPTY_CONFIG, delegate, new DefaultObjectNameGenerator());
    relationshipLoader.load(metaData, store);
    Collection<DbRelationship> rels = getDbEntity("ARTIST").getRelationships();
    assertNotNull(rels);
    assertTrue(!rels.isEmpty());
    // test one-to-one
    rels = getDbEntity("PAINTING").getRelationships();
    assertNotNull(rels);
    // find relationship to PAINTING_INFO
    DbRelationship oneToOne = null;
    for (DbRelationship rel : rels) {
        if ("PAINTING_INFO".equalsIgnoreCase(rel.getTargetEntityName())) {
            oneToOne = rel;
            break;
        }
    }
    assertNotNull("No relationship to PAINTING_INFO", oneToOne);
    assertFalse("Relationship to PAINTING_INFO must be to-one", oneToOne.isToMany());
    assertTrue("Relationship to PAINTING_INFO must be to-one", oneToOne.isToDependentPK());
}
Also used : DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) DbRelationship(org.apache.cayenne.map.DbRelationship) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Aggregations

DbRelationship (org.apache.cayenne.map.DbRelationship)106 DbEntity (org.apache.cayenne.map.DbEntity)59 DbAttribute (org.apache.cayenne.map.DbAttribute)35 DbJoin (org.apache.cayenne.map.DbJoin)35 ObjEntity (org.apache.cayenne.map.ObjEntity)30 ObjRelationship (org.apache.cayenne.map.ObjRelationship)28 ObjAttribute (org.apache.cayenne.map.ObjAttribute)20 Test (org.junit.Test)15 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)13 ArrayList (java.util.ArrayList)11 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)10 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)9 DataMap (org.apache.cayenne.map.DataMap)9 Entity (org.apache.cayenne.map.Entity)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 HashMap (java.util.HashMap)6 ObjectId (org.apache.cayenne.ObjectId)6