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();
}
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;
}
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);
}
}
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);
}
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);
}
Aggregations