public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) { ObjEntity entity = resolver.getObjEntity(((ObjectId) nodeId).getEntityName()); ObjRelationship relationship = (ObjRelationship) entity.getRelationship(arcId.toString()); if (relationship.isSourceIndependentFromTargetChange()) { // do not record temporary id mods... if (!((ObjectId) nodeId).isTemporary()) { indirectModifications.add(nodeId); } if (relationship.isFlattened()) { if (relationship.isReadOnly()) { throw new CayenneRuntimeException( "Cannot unset the read-only flattened relationship " + relationship.getName()); } // Register this combination (so we can remove it later if an insert // occurs before commit) FlattenedArcKey key = new FlattenedArcKey((ObjectId) nodeId, (ObjectId) targetNodeId, relationship); // If this combination has already been inserted, simply "uninsert" it // also do not delete it twice if (!flattenedInserts.remove(key)) { flattenedDeletes.add(key); } } } }
/** * Simule la présence d'une valeur. * * <p>Seules les relations toOne sont supportées. * * @param property La propriété à simuler. */ public void fake(String property) { // Test de la propriété ObjEntity ent = objEntity(); ObjRelationship rel = (ObjRelationship) ent.getRelationship(property); if (rel == null) { throw new IllegalArgumentException("Pas de relation " + ent.getName() + "." + property); } if (rel.isToMany()) { throw new IllegalArgumentException( "La relation " + ent.getName() + "." + property + " n'est pas un toOne"); } ObjEntity targetEntity = (ObjEntity) rel.getTargetEntity(); // Ok, ajout. fakes.put(property, targetEntity.getJavaClass()); }
/** Creates an accessor to read a map key for a given relationship. */ protected Accessor createMapKeyAccessor( ObjRelationship relationship, ClassDescriptor targetDescriptor) { String mapKey = relationship.getMapKey(); if (mapKey != null) { return new PropertyAccessor(targetDescriptor.getProperty(mapKey)); } return IdMapKeyAccessor.SHARED_ACCESSOR; }
public void testForeignKey() throws Exception { dropTableIfPresent("NEW_TABLE"); dropTableIfPresent("NEW_TABLE2"); assertTokensAndExecute(0, 0); DbEntity dbEntity1 = new DbEntity("NEW_TABLE"); DbAttribute e1col1 = new DbAttribute("ID", Types.INTEGER, dbEntity1); e1col1.setMandatory(true); e1col1.setPrimaryKey(true); dbEntity1.addAttribute(e1col1); DbAttribute e1col2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity1); e1col2.setMaxLength(10); e1col2.setMandatory(false); dbEntity1.addAttribute(e1col2); map.addDbEntity(dbEntity1); DbEntity dbEntity2 = new DbEntity("NEW_TABLE2"); DbAttribute e2col1 = new DbAttribute("ID", Types.INTEGER, dbEntity2); e2col1.setMandatory(true); e2col1.setPrimaryKey(true); dbEntity2.addAttribute(e2col1); DbAttribute e2col2 = new DbAttribute("FK", Types.INTEGER, dbEntity2); dbEntity2.addAttribute(e2col2); DbAttribute e2col3 = new DbAttribute("NAME", Types.VARCHAR, dbEntity2); e2col3.setMaxLength(10); dbEntity2.addAttribute(e2col3); map.addDbEntity(dbEntity2); // create db relationships DbRelationship rel1To2 = new DbRelationship("rel1To2"); rel1To2.setSourceEntity(dbEntity1); rel1To2.setTargetEntity(dbEntity2); rel1To2.setToMany(true); rel1To2.addJoin(new DbJoin(rel1To2, e1col1.getName(), e2col2.getName())); dbEntity1.addRelationship(rel1To2); DbRelationship rel2To1 = new DbRelationship("rel2To1"); rel2To1.setSourceEntity(dbEntity2); rel2To1.setTargetEntity(dbEntity1); rel2To1.setToMany(false); rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName())); dbEntity2.addRelationship(rel2To1); assertSame(rel1To2, rel2To1.getReverseRelationship()); assertSame(rel2To1, rel1To2.getReverseRelationship()); assertTokensAndExecute(4, 0); assertTokensAndExecute(0, 0); // create ObjEntities ObjEntity objEntity1 = new ObjEntity("NewTable"); objEntity1.setDbEntity(dbEntity1); ObjAttribute oatr1 = new ObjAttribute("name"); oatr1.setDbAttributePath(e1col2.getName()); oatr1.setType("java.lang.String"); objEntity1.addAttribute(oatr1); map.addObjEntity(objEntity1); ObjEntity objEntity2 = new ObjEntity("NewTable2"); objEntity2.setDbEntity(dbEntity2); ObjAttribute o2a1 = new ObjAttribute("name"); o2a1.setDbAttributePath(e2col3.getName()); o2a1.setType("java.lang.String"); objEntity2.addAttribute(o2a1); map.addObjEntity(objEntity2); // create ObjRelationships assertEquals(0, objEntity1.getRelationships().size()); assertEquals(0, objEntity2.getRelationships().size()); ObjRelationship objRel1To2 = new ObjRelationship("objRel1To2"); objRel1To2.addDbRelationship(rel1To2); objRel1To2.setSourceEntity(objEntity1); objRel1To2.setTargetEntity(objEntity2); objEntity1.addRelationship(objRel1To2); ObjRelationship objRel2To1 = new ObjRelationship("objRel2To1"); objRel2To1.addDbRelationship(rel2To1); objRel2To1.setSourceEntity(objEntity2); objRel2To1.setTargetEntity(objEntity1); objEntity2.addRelationship(objRel2To1); assertEquals(1, objEntity1.getRelationships().size()); assertEquals(1, objEntity2.getRelationships().size()); assertSame(objRel1To2, objRel2To1.getReverseRelationship()); assertSame(objRel2To1, objRel1To2.getReverseRelationship()); // remove relationship and fk from model, merge to db and read to model dbEntity2.removeRelationship(rel2To1.getName()); dbEntity1.removeRelationship(rel1To2.getName()); dbEntity2.removeAttribute(e2col2.getName()); List<MergerToken> tokens = createMergeTokens(); assertTokens(tokens, 2, 1); for (MergerToken token : tokens) { if (token.getDirection().isToDb()) { execute(token); } } assertTokensAndExecute(0, 0); dbEntity2.addRelationship(rel2To1); dbEntity1.addRelationship(rel1To2); dbEntity2.addAttribute(e2col2); // try do use the merger to remove the relationship in the model tokens = createMergeTokens(); assertTokens(tokens, 2, 0); // TODO: reversing the following two tokens should also reverse the order MergerToken token0 = tokens.get(0).createReverse(mergerFactory()); MergerToken token1 = tokens.get(1).createReverse(mergerFactory()); if (!(token0 instanceof DropRelationshipToModel && token1 instanceof DropColumnToModel || token1 instanceof DropRelationshipToModel && token0 instanceof DropColumnToModel)) { fail(); } execute(token0); execute(token1); // check after merging assertNull(dbEntity2.getAttribute(e2col2.getName())); assertEquals(0, dbEntity1.getRelationships().size()); assertEquals(0, dbEntity2.getRelationships().size()); assertEquals(0, objEntity1.getRelationships().size()); assertEquals(0, objEntity2.getRelationships().size()); // clear up dbEntity1.removeRelationship(rel1To2.getName()); dbEntity2.removeRelationship(rel2To1.getName()); map.removeObjEntity(objEntity1.getName(), true); map.removeDbEntity(dbEntity1.getName(), true); map.removeObjEntity(objEntity2.getName(), true); map.removeDbEntity(dbEntity2.getName(), true); resolver.refreshMappingCache(); assertNull(map.getObjEntity(objEntity1.getName())); assertNull(map.getDbEntity(dbEntity1.getName())); assertNull(map.getObjEntity(objEntity2.getName())); assertNull(map.getDbEntity(dbEntity2.getName())); assertFalse(map.getDbEntities().contains(dbEntity1)); assertFalse(map.getDbEntities().contains(dbEntity2)); assertTokensAndExecute(2, 0); assertTokensAndExecute(0, 0); }
protected ClassDescriptor getDescriptor(ObjEntity entity, Class<?> entityClass) { String superEntityName = entity.getSuperEntityName(); ClassDescriptor superDescriptor = (superEntityName != null) ? descriptorMap.getDescriptor(superEntityName) : null; PersistentDescriptor descriptor = createDescriptor(); descriptor.setEntity(entity); descriptor.setSuperclassDescriptor(superDescriptor); descriptor.setObjectClass(entityClass); descriptor.setPersistenceStateAccessor( new BeanAccessor(entityClass, "persistenceState", Integer.TYPE)); // only include this entity attributes and skip superclasses... for (ObjAttribute attribute : descriptor.getEntity().getDeclaredAttributes()) { if (attribute instanceof EmbeddedAttribute) { EmbeddedAttribute embedded = (EmbeddedAttribute) attribute; for (ObjAttribute objAttribute : embedded.getAttributes()) { createEmbeddedAttributeProperty(descriptor, embedded, objAttribute); } } else { createAttributeProperty(descriptor, attribute); } } // only include this entity relationships and skip superclasses... for (ObjRelationship relationship : descriptor.getEntity().getDeclaredRelationships()) { if (relationship.isToMany()) { String collectionType = relationship.getCollectionType(); if (collectionType == null || ObjRelationship.DEFAULT_COLLECTION_TYPE.equals(collectionType)) { createToManyListProperty(descriptor, relationship); } else if (collectionType.equals("java.util.Map")) { createToManyMapProperty(descriptor, relationship); } else if (collectionType.equals("java.util.Set")) { createToManySetProperty(descriptor, relationship); } else if (collectionType.equals("java.util.Collection")) { createToManyCollectionProperty(descriptor, relationship); } else { throw new IllegalArgumentException( "Unsupported to-many collection type: " + collectionType); } } else { createToOneProperty(descriptor, relationship); } } EntityInheritanceTree inheritanceTree = descriptorMap.getResolver().getInheritanceTree(descriptor.getEntity().getName()); descriptor.setEntityInheritanceTree(inheritanceTree); indexSubclassDescriptors(descriptor, inheritanceTree); indexQualifiers(descriptor, inheritanceTree); appendDeclaredRootDbEntity(descriptor, descriptor.getEntity()); indexRootDbEntities(descriptor, inheritanceTree); indexSuperclassProperties(descriptor); descriptor.sortProperties(); return descriptor; }