DbEntity dbEntity = map.getDbEntity("PAINTING");
assertNotNull(dbEntity);
ObjEntity objEntity = map.getObjEntity("Painting");
assertNotNull(objEntity);
DataContext ctxt = createDataContext();
// insert some rows before adding "not null" column
final int nrows = 10;
for (int i = 0; i < nrows; i++) {
DataObject o = (DataObject) ctxt.newObject("Painting");
o.writeProperty("paintingTitle", "ptitle" + i);
}
ctxt.commitChanges();
// 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);
assertTrue(dbEntity.getAttributes().contains(column));
assertEquals(column, dbEntity.getAttribute(column.getName()));
assertTokensAndExecute(node, map, 1, 0);
// need obj attr to be able to query
ObjAttribute objAttr = new ObjAttribute("newcol2");
objAttr.setDbAttributePath(column.getName());
objEntity.addAttribute(objAttr);
// check that is was merged
assertTokensAndExecute(node, map, 0, 0);
// set not null
column.setMandatory(true);
// merge to db
assertTokensAndExecute(node, map, 2, 0);
// check that is was merged
assertTokensAndExecute(node, map, 0, 0);
// check values for null
Expression qual = ExpressionFactory.matchExp(
objAttr.getName(),
DEFAULT_VALUE_STRING);
SelectQuery query = new SelectQuery("Painting", qual);
List<Persistent> rows = ctxt.performQuery(query);
assertEquals(nrows, rows.size());
// clean up
dbEntity.removeAttribute(column.getName());
assertTokensAndExecute(node, map, 1, 0);