Resurrecting this thread...

On 12/11/2008, at 1:57 AM, Andrey Razumovsky wrote:

Thanks, it works! Here's the snippet:

String cloneName = objEntity.getName() + "_no_qualifier";
ObjEntity clone = dataMap.getObjEntity(cloneName);
if (clone == null) {
clone = new ObjEntity(cloneName);
clone.setDbEntity(objEntity.getDbEntity());
objEntity.getDataMap().addObjEntity(clone);
}

Works on every Cayenne

Isn't there more to it than this?

What javaClass are you mapping to? I didn't think the cloned objentity could map to the same java class (given that the map allows you to look up via javaclass). And what about relationships .. do you need to recursively re-define relations so that they also map to a similarly cloned entity?

I'm about to attempt to do this as listed below[1] .. so any feedback welcome.

2008/11/11, Andrus Adamchik <[email protected]>:
On Nov 11, 2008, at 2:15 PM, Andrey Razumovsky wrote:

ObjEntity clone = (ObjEntity) SerializationUtils.clone(objEntity);
clone.setDeclaredQualifier(null);
SelectQuery query = new SelectQuery(clone);


In addition you'll need to rename the entity and register it with the
DataMap. So you might as well map an alt. entity via the Modeler.

Or use a separate stack all together to work with a parallel set of
entities without qualifiers.

Andrus, any clues on how one goes about creating a separate stack? Is there a simple way of finding the default stack and cloning it so as to recurse through the entities to turn off the declared qual?

with regards,
--

Lachlan Deck

[1] any improvements welcome...

private static synchronized ObjEntity registeredNonRestrictingForObjEntity(DataMap map, ObjEntity originalEntity) {
        String cloneName = originalEntity.getName() + "_nonRestricting";
        ObjEntity cloneEntity = map.getObjEntity(cloneName);
        if (cloneEntity == null) {
                cloneEntity = new ObjEntity(cloneName);
                cloneEntity.setDbEntity(originalEntity.getDbEntity());
                cloneEntity.setDeclaredQualifier(null);
cloneEntity.setClassName(originalEntity.getClassName() + "$NonRestricting");

                // define attributes
                cloneEntity.clearAttributes();
for (Entry<String, ObjAttribute> attribute : originalEntity.getAttributeMap().entrySet()) { ObjAttribute att = (ObjAttribute) SerializationUtils.clone(attribute.getValue());
                        att.setEntity(cloneEntity);
                        cloneEntity.addAttribute(att);
                }

                map.addObjEntity(cloneEntity);

                // define relations
                cloneEntity.clearRelationships();
SortedMap<String, ObjRelationship> relations = cloneEntity.getRelationshipMap();
                if (relations != null) {
for (Entry<String, ObjRelationship> relation : relations.entrySet()) {
                                ObjRelationship rel = relation.getValue();
ObjEntity targetEntity = registeredNonRestrictingForObjEntity(map, (ObjEntity) relation.getValue().getTargetEntity());

ObjRelationship relationClone = (ObjRelationship) SerializationUtils.clone(rel);
                                relationClone.setTargetEntity(targetEntity);
                                
cloneEntity.removeRelationship(relation.getKey());
                                cloneEntity.addRelationship(relationClone);
                        }
                }
        }
        return cloneEntity;
}

Reply via email to