Search in sources :

Example 21 with DbEntity

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

the class EJBQLTableId method getDbEntity.

/**
 * Returns a DbEntity corresponding to the ID, that could be a root entity for the id,
 * or a joined entity.
 */
DbEntity getDbEntity(EJBQLTranslationContext context) {
    ClassDescriptor descriptor = context.getEntityDescriptor(entityId);
    DbEntity rootEntity = descriptor.getEntity().getDbEntity();
    if (dbPath == null) {
        return rootEntity;
    }
    DbRelationship r = null;
    Iterator<?> it = rootEntity.resolvePathComponents(dbPath);
    while (it.hasNext()) {
        r = (DbRelationship) it.next();
    }
    return (DbEntity) r.getTargetEntity();
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 22 with DbEntity

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

the class EJBQLTranslationContext method getIncomingRelationships.

List<DbRelationship> getIncomingRelationships(EJBQLTableId id) {
    List<DbRelationship> incoming = compiledExpression.getIncomingRelationships(resolveId(id.getEntityId()));
    // append tail of flattened relationships...
    if (id.getDbPath() != null) {
        DbEntity entity;
        if (incoming == null || incoming.isEmpty()) {
            entity = compiledExpression.getEntityDescriptor(id.getEntityId()).getEntity().getDbEntity();
        } else {
            DbRelationship last = incoming.get(incoming.size() - 1);
            entity = last.getTargetEntity();
        }
        incoming = new ArrayList<>(incoming);
        Iterator<?> it = entity.resolvePathComponents(id.getDbPath());
        while (it.hasNext()) {
            incoming.add((DbRelationship) it.next());
        }
    }
    return incoming;
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 23 with DbEntity

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

the class SetAllowNullToDbIT method test.

@Test
public void test() throws Exception {
    DbEntity dbEntity = map.getDbEntity("PAINTING");
    assertNotNull(dbEntity);
    // create and add new column to model and db
    DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);
    try {
        column.setMandatory(true);
        column.setMaxLength(10);
        dbEntity.addAttribute(column);
        assertTokensAndExecute(2, 0);
        // check that is was merged
        assertTokensAndExecute(0, 0);
        // set null
        column.setMandatory(false);
        // merge to db
        assertTokensAndExecute(1, 0);
        // check that is was merged
        assertTokensAndExecute(0, 0);
    // clean up
    } finally {
        dbEntity.removeAttribute(column.getName());
        assertTokensAndExecute(1, 0);
        assertTokensAndExecute(0, 0);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 24 with DbEntity

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

the class SetGeneratedFlagToDbIT method dropGeneratedFlag.

@Test
public void dropGeneratedFlag() throws Exception {
    DbEntity dbEntity = createTestTable(true);
    assertNotNull(dbEntity);
    DbAttribute attribute = dbEntity.getAttribute("ID");
    assertNotNull(attribute);
    assertTrue(attribute.isGenerated());
    attribute.setGenerated(false);
    List<MergerToken> tokens = createMergeTokens();
    if (!dbAdapter.supportsGeneratedKeys()) {
        assertEquals(0, tokens.size());
        return;
    }
    assertEquals(1, tokens.size());
    MergerToken token = tokens.get(0);
    assertTrue(token instanceof SetGeneratedFlagToDb);
    try {
        execute(token);
        if (!dbAdapter.supportsGeneratedKeysDrop()) {
            fail("SetGeneratedFlagToDb should fail on current DB");
        }
    } catch (UnsupportedOperationException ignored) {
        return;
    }
    assertTokensAndExecute(0, 0);
}
Also used : 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 25 with DbEntity

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

the class SetNotNullToDbIT method test.

@Test
public void test() throws Exception {
    DbEntity dbEntity = map.getDbEntity("PAINTING");
    assertNotNull(dbEntity);
    // create and add new column to model and db
    DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);
    column.setMandatory(false);
    column.setMaxLength(10);
    dbEntity.addAttribute(column);
    assertTokensAndExecute(1, 0);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // set not null
    column.setMandatory(true);
    // merge to db
    assertTokensAndExecute(1, 0);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // clean up
    dbEntity.removeAttribute(column.getName());
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Aggregations

DbEntity (org.apache.cayenne.map.DbEntity)273 DbAttribute (org.apache.cayenne.map.DbAttribute)106 Test (org.junit.Test)106 ObjEntity (org.apache.cayenne.map.ObjEntity)64 DbRelationship (org.apache.cayenne.map.DbRelationship)55 DataMap (org.apache.cayenne.map.DataMap)47 ObjAttribute (org.apache.cayenne.map.ObjAttribute)26 ArrayList (java.util.ArrayList)25 DbJoin (org.apache.cayenne.map.DbJoin)24 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)20 ObjRelationship (org.apache.cayenne.map.ObjRelationship)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)16 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)16 Entity (org.apache.cayenne.map.Entity)16 List (java.util.List)15 DbAdapter (org.apache.cayenne.dba.DbAdapter)15 EntityEvent (org.apache.cayenne.map.event.EntityEvent)14 HashMap (java.util.HashMap)12 SelectQuery (org.apache.cayenne.query.SelectQuery)12 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)11