Well, that code has changed since then :) Currently I do the following (much
like your code)
String cloneName = objEntity.getName() + OBJENTITY_POSTFIX;
ObjEntity clone = dataMap.getObjEntity(cloneName);
if (clone == null) {
clone = new ObjEntity(cloneName);
clone.setDbEntity(objEntity.getDbEntity());
clone.setClassName(objEntity.getClassName());
for (ObjAttribute attr : objEntity.getAttributes()) {
ObjAttribute attrClone = new
ObjAttribute(attr.getName());
attrClone.setDbAttributePath(attr.getDbAttributePath());
clone.addAttribute(attrClone);
}
for (ObjRelationship rel : objEntity.getRelationships())
{
ObjRelationship relClone = new
ObjRelationship(rel.getName());
relClone.setSourceEntity(rel.getSourceEntity());
relClone.setTargetEntity(rel.getTargetEntity());
relClone.setDbRelationshipPath(rel.getDbRelationshipPath());
clone.addRelationship(relClone);
}
objEntity.getDataMap().addObjEntity(clone);
}
That *almost* works. It fails when other thread is trying to perform query
with root=ObjEntity's CDO class (because two entities are mapped to same
class). I'm fine with this implementation by now. But ideally you need to
clone whole DataMap.
2009/4/29 Lachlan Deck <lachlan.dec..mail.com>
> 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 <andru..bjectstyle.org>:
>>
>>> 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;
> }
>
>
This archive was generated by hypermail 2.0.0 : Wed Apr 29 2009 - 03:56:31 EDT