cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Kienenberger <mkien...@gmail.com>
Subject Re: Re: Cloning a CayenneDataObject
Date Tue, 10 Feb 2009 13:55:43 GMT
You can either implement Clonable on the individual entity, or
implement it on a superclass of all entities, or implement it in your
save-as method.   It depends how unique your cloning situation is, ie,
whether you are cloning all attributes and relationships consistently.

As Andrey mentioned, the most generic way to clone is to iterate
through the ObjAttributes and ObjRelationships and set each property
as you come across it.   This way, you don't have to perform any
maintenance on the method if your model changes.   The implementation
(at least back in Cayenne 2.0 and earlier) would be to get an
EntityResolver from either the DataContext/ObjectContext or DataMap or
DataDomain, and then find the ObjEntity for the current entity's
class.

If the behavior is always the same for a specific entity, you can use
Andrey's suggestion of inclusion/exclusion getters on each entity.
If it varies depending on the use, you would write and call
clone(inclusion/exclusion attributes/relationships)

On Tue, Feb 10, 2009 at 8:35 AM, Paul Logasa Bogen II <plb@tamu.edu> wrote:
> Sorry for the long time to respond, I realized this morning that I hadn't
> actually joined the list yet (which I have rectified) and didn't get your
> response when you sent it.
>
> Do give a better picture of what I am doing, I have is multiple users each
> with multiple collections each of the collections have multiple entries.
> When I hit "Save As", I need to clone a working collection and all of its
> entries.
>
> If I understand you correctly, then I should make a custom clone method that
> clones only what I want it to.
>
> plb
>
> Andrey Razumovsky wrote:
>>
>> Hi Paul,
>>
>> Do you need to clone only the object's attributes (i.e. fields from one DB
>> table) or you need to clone related objects too? If first, I'd recommend
>> to
>> iterate through ObjEntity attributes and write their values to new object.
>> Tell me if you need a snippet.
>> If second, since you likely don't want to clone *all* relationships,
>> because
>> this might result in cloning all database, you should write a clone method
>> which clones only rels specified in DataObject, e.g.
>>
>> interface CayenneCloneable {
>>   public List<String> getClonedRelationships();
>> }
>>
>> class Artist extends _Artist implements CayenneCloneable {
>>  ...
>> }
>>
>> //somewhere
>> public static <T extends CayenneDataObject> T clone(T src) {
>>  //iterate through obj attributes
>>
>>  if (T instance of CayenneCloneable) {
>>     //recursively clone relationships
>>  }
>> }
>>
>> That's how I've done it, and I can search my projects for an example if
>> you
>> want it
>>
>> Regards,
>> Andrey
>>
>> 2009/2/9 Paul Logasa Bogen II <plb@tamu.edu>
>>
>>
>>>
>>> I am writing a web application with Wicket on the front end and Cayenne
>>> on
>>> the back end. When a user clicks "Save As" in my application I want to
>>> save
>>> the current working object with a new key. My suspicion is if I just
>>> blank
>>> out the key and do a context.commitChanges() I will just change the key
>>> and
>>> any other information for the object instead of creating new rows in the
>>> database. Is this correct? And what is the recommended way to clone or
>>> copy
>>> an object to a new one?
>>>
>>> plb
>>>
>>>
>>
>>
>
>

Mime
View raw message