use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.
the class CayenneContextIT method testDeleteObject.
@Test
public void testDeleteObject() {
CayenneContext context = new CayenneContext(new MockDataChannel());
ObjEntity entity = new ObjEntity("test_entity");
entity.setClassName(MockPersistentObject.class.getName());
DataMap dataMap = new DataMap("test");
dataMap.addObjEntity(entity);
Collection<DataMap> entities = Collections.singleton(dataMap);
context.setEntityResolver(new EntityResolver(entities));
// TRANSIENT ... should quietly ignore it
Persistent transientObject = new MockPersistentObject();
context.deleteObjects(transientObject);
assertEquals(PersistenceState.TRANSIENT, transientObject.getPersistenceState());
// NEW ... should make it TRANSIENT
// create via context to make sure that object store would register it
Persistent newObject = context.newObject(MockPersistentObject.class);
assertNotNull(newObject.getObjectContext());
context.deleteObjects(newObject);
assertNull(newObject.getObjectContext());
assertEquals(PersistenceState.TRANSIENT, newObject.getPersistenceState());
assertFalse(context.internalGraphManager().dirtyNodes().contains(newObject.getObjectId()));
// see CAY-547 for details...
assertFalse(context.internalGraphManager().dirtyNodes().contains(null));
// COMMITTED
Persistent committed = new MockPersistentObject();
committed.setPersistenceState(PersistenceState.COMMITTED);
committed.setObjectId(ObjectId.of("test_entity", "key", "value1"));
committed.setObjectContext(context);
context.deleteObjects(committed);
assertEquals(PersistenceState.DELETED, committed.getPersistenceState());
// MODIFIED
Persistent modified = new MockPersistentObject();
modified.setPersistenceState(PersistenceState.MODIFIED);
modified.setObjectId(ObjectId.of("test_entity", "key", "value2"));
modified.setObjectContext(context);
context.deleteObjects(modified);
assertEquals(PersistenceState.DELETED, modified.getPersistenceState());
// DELETED
Persistent deleted = new MockPersistentObject();
deleted.setPersistenceState(PersistenceState.DELETED);
deleted.setObjectId(ObjectId.of("test_entity", "key", "value3"));
deleted.setObjectContext(context);
context.deleteObjects(deleted);
assertEquals(PersistenceState.DELETED, committed.getPersistenceState());
}
use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.
the class CayenneContextIT method testNewObject.
@Test
public void testNewObject() {
CayenneContext context = new CayenneContext(new MockDataChannel());
ObjEntity entity = new ObjEntity("test_entity");
entity.setClassName(MockPersistentObject.class.getName());
DataMap dataMap = new DataMap("test");
dataMap.addObjEntity(entity);
Collection<DataMap> entities = Collections.singleton(dataMap);
context.setEntityResolver(new EntityResolver(entities));
Persistent object = context.newObject(MockPersistentObject.class);
assertNotNull(object);
assertTrue(object instanceof MockPersistentObject);
assertEquals(PersistenceState.NEW, object.getPersistenceState());
assertSame(context, object.getObjectContext());
assertTrue(context.internalGraphManager().dirtyNodes(PersistenceState.NEW).contains(object));
assertNotNull(object.getObjectId());
assertTrue(object.getObjectId().isTemporary());
}
use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.
the class ClientServerChannelIT method testGetEntityResolver.
@Test
public void testGetEntityResolver() {
EntityResolver resolver = clientServerChannel.getEntityResolver();
assertNotNull(resolver);
assertNull(resolver.getObjEntity(ClientMtTable1.class));
assertNotNull(resolver.getClientEntityResolver().getObjEntity(ClientMtTable1.class));
}
use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.
the class ObjectContextChangeLogTest method testGetDiffsSerializableWithHessian.
@Test
public void testGetDiffsSerializableWithHessian() throws Exception {
ObjectContextChangeLog recorder = new ObjectContextChangeLog();
// id must be a serializable object
recorder.addOperation(new NodeCreateOperation("id-string"));
CompoundDiff diff = (CompoundDiff) recorder.getDiffs();
Object clone = HessianUtil.cloneViaClientServerSerialization(diff, new EntityResolver());
assertNotNull(clone);
assertTrue(clone instanceof CompoundDiff);
CompoundDiff d1 = (CompoundDiff) clone;
assertEquals(1, d1.getDiffs().size());
}
use of org.apache.cayenne.map.EntityResolver in project cayenne by apache.
the class DbGenerator method prepareDbEntities.
/**
* Helper method that orders DbEntities to satisfy referential constraints
* and returns an ordered list. It also filters out DerivedDbEntities.
*/
private void prepareDbEntities(Collection<DbEntity> excludedEntities) {
if (excludedEntities == null) {
excludedEntities = Collections.emptyList();
}
List<DbEntity> tables = new ArrayList<>();
List<DbEntity> tablesWithAutoPk = new ArrayList<>();
for (DbEntity nextEntity : map.getDbEntities()) {
// tables with no columns are not included
if (nextEntity.getAttributes().size() == 0) {
logObj.info("Skipping entity with no attributes: " + nextEntity.getName());
continue;
}
// check if this entity is explicitly excluded
if (excludedEntities.contains(nextEntity)) {
continue;
}
// tables with invalid DbAttributes are not included
boolean invalidAttributes = false;
for (final DbAttribute attr : nextEntity.getAttributes()) {
if (attr.getType() == TypesMapping.NOT_DEFINED) {
logObj.info("Skipping entity, attribute type is undefined: " + nextEntity.getName() + "." + attr.getName());
invalidAttributes = true;
break;
}
}
if (invalidAttributes) {
continue;
}
tables.add(nextEntity);
// check if an automatic PK generation can be potentially supported
// in this entity. For now simply check that the key is not
// propagated
Iterator<DbRelationship> relationships = nextEntity.getRelationships().iterator();
// create a copy of the original PK list,
// since the list will be modified locally
List<DbAttribute> pkAttributes = new ArrayList<>(nextEntity.getPrimaryKeys());
while (pkAttributes.size() > 0 && relationships.hasNext()) {
DbRelationship nextRelationship = relationships.next();
if (!nextRelationship.isToMasterPK()) {
continue;
}
// so
for (DbJoin join : nextRelationship.getJoins()) {
pkAttributes.remove(join.getSource());
}
}
// is not propagated via relationship
if (pkAttributes.size() > 0) {
tablesWithAutoPk.add(nextEntity);
}
}
// sort table list
if (tables.size() > 1) {
EntitySorter sorter = new AshwoodEntitySorter();
sorter.setEntityResolver(new EntityResolver(Collections.singleton(map)));
sorter.sortDbEntities(tables, false);
}
this.dbEntitiesInInsertOrder = tables;
this.dbEntitiesRequiringAutoPK = tablesWithAutoPk;
}
Aggregations