Search in sources :

Example 71 with ObjEntity

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

the class ManyToManyCandidateEntityTest method testOptimisationForManyToManyEntity.

@Test
public void testOptimisationForManyToManyEntity() {
    ObjEntity manyToManyEntity = map.getObjEntity("Table1Table2");
    ManyToManyCandidateEntity.build(manyToManyEntity).optimizeRelationships(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()));
    ObjEntity table1Entity = map.getObjEntity("Table1");
    ObjEntity table2Entity = map.getObjEntity("Table2");
    assertEquals(1, table1Entity.getRelationships().size());
    assertEquals(table2Entity, new ArrayList<Relationship>(table1Entity.getRelationships()).get(0).getTargetEntity());
    assertEquals(1, table2Entity.getRelationships().size());
    assertEquals(table1Entity, new ArrayList<Relationship>(table2Entity.getRelationships()).get(0).getTargetEntity());
}
Also used : DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) ObjEntity(org.apache.cayenne.map.ObjEntity) Relationship(org.apache.cayenne.map.Relationship) Test(org.junit.Test)

Example 72 with ObjEntity

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

the class CreateTableToModelIT method testAddTable.

@Test
public void testAddTable() throws Exception {
    dropTableIfPresent("NEW_TABLE");
    assertTokensAndExecute(0, 0);
    DbEntity dbEntity = new DbEntity("NEW_TABLE");
    DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
    column1.setMandatory(true);
    column1.setPrimaryKey(true);
    dbEntity.addAttribute(column1);
    DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
    column2.setMaxLength(10);
    column2.setMandatory(false);
    dbEntity.addAttribute(column2);
    // for the new entity to the db
    execute(mergerFactory().createCreateTableToDb(dbEntity));
    List<MergerToken> tokens = createMergeTokens();
    assertEquals(1, tokens.size());
    MergerToken token = tokens.get(0);
    if (token.getDirection().isToDb()) {
        token = token.createReverse(mergerFactory());
    }
    assertTrue(token.getClass().getName(), token instanceof CreateTableToModel);
    execute(token);
    ObjEntity objEntity = null;
    for (ObjEntity candidate : map.getObjEntities()) {
        if (dbEntity.getName().equalsIgnoreCase(candidate.getDbEntityName())) {
            objEntity = candidate;
            break;
        }
    }
    assertNotNull(objEntity);
    assertEquals(objEntity.getClassName(), map.getDefaultPackage() + "." + objEntity.getName());
    assertEquals(objEntity.getSuperClassName(), map.getDefaultSuperclass());
    assertEquals(objEntity.getClientClassName(), map.getDefaultClientPackage() + "." + objEntity.getName());
    assertEquals(objEntity.getClientSuperClassName(), map.getDefaultClientSuperclass());
    assertEquals(1, objEntity.getAttributes().size());
    assertEquals("java.lang.String", objEntity.getAttributes().iterator().next().getType());
    // clear up
    // fix psql case issue
    map.removeDbEntity(objEntity.getDbEntity().getName(), true);
    map.removeObjEntity(objEntity.getName(), true);
    map.removeDbEntity(dbEntity.getName(), true);
    resolver.refreshMappingCache();
    assertNull(map.getObjEntity(objEntity.getName()));
    assertNull(map.getDbEntity(dbEntity.getName()));
    assertFalse(map.getDbEntities().contains(dbEntity));
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) Test(org.junit.Test)

Example 73 with ObjEntity

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

the class NameBuilderTest method testName_Callbacks_ObjEntityContext.

@Test
public void testName_Callbacks_ObjEntityContext() {
    ObjEntity entity = new ObjEntity();
    String c0 = NameBuilder.builderForCallbackMethod(entity).name();
    assertEquals("onEvent", c0);
    entity.getCallbackMap().getPostAdd().addCallbackMethod(c0);
    String c1 = NameBuilder.builderForCallbackMethod(entity).name();
    assertEquals("onEvent1", c1);
    entity.getCallbackMap().getPostAdd().addCallbackMethod(c1);
    entity.addAttribute(new ObjAttribute("untitledAttr"));
    String c3 = NameBuilder.builderForCallbackMethod(entity).baseName("getUntitledAttr").name();
    assertEquals("getUntitledAttr1", c3);
    entity.getCallbackMap().getPostAdd().addCallbackMethod(c3);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) Test(org.junit.Test)

Example 74 with ObjEntity

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

the class DataDomainIndirectDiffBuilder method processArcChange.

private void processArcChange(ObjectId nodeId, ArcId arcId) {
    ObjEntity entity = resolver.getObjEntity(nodeId.getEntityName());
    ObjRelationship relationship = entity.getRelationship(arcId.getForwardArc());
    if (relationship != null && relationship.isSourceIndependentFromTargetChange()) {
        // do not record temporary id mods...
        if (!nodeId.isTemporary()) {
            if (indirectModifications == null) {
                indirectModifications = new HashSet<>();
            }
            indirectModifications.add(nodeId);
        }
        if (relationship.isFlattened() && relationship.isReadOnly()) {
            throw new CayenneRuntimeException("Cannot change the read-only flattened relationship %s in ObjEntity '%s'.", relationship.getName(), relationship.getSourceEntity().getName());
        }
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 75 with ObjEntity

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

the class InheritanceAwareEntityRowReader method postprocessRow.

@Override
void postprocessRow(ResultSet resultSet, DataRow dataRow) throws Exception {
    if (postProcessor != null) {
        postProcessor.postprocessRow(resultSet, dataRow);
    }
    ObjEntity entity = entityInheritanceTree.entityMatchingRow(dataRow);
    dataRow.setEntityName(entity != null ? entity.getName() : entityName);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity)

Aggregations

ObjEntity (org.apache.cayenne.map.ObjEntity)294 Test (org.junit.Test)110 DbEntity (org.apache.cayenne.map.DbEntity)72 ObjAttribute (org.apache.cayenne.map.ObjAttribute)68 ObjRelationship (org.apache.cayenne.map.ObjRelationship)62 DataMap (org.apache.cayenne.map.DataMap)57 DbAttribute (org.apache.cayenne.map.DbAttribute)37 DbRelationship (org.apache.cayenne.map.DbRelationship)29 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)27 ObjectId (org.apache.cayenne.ObjectId)26 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)22 ArrayList (java.util.ArrayList)19 Embeddable (org.apache.cayenne.map.Embeddable)18 EntityResolver (org.apache.cayenne.map.EntityResolver)17 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)16 Expression (org.apache.cayenne.exp.Expression)15 Persistent (org.apache.cayenne.Persistent)12 EntityEvent (org.apache.cayenne.map.event.EntityEvent)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 Entity (org.apache.cayenne.map.Entity)11