I am forwarding this to the list, since others may find it useful too...
Sorry, it was indeed a bad advise on my part. I wasn't taking into
account that there might not be an existing DB row for a given Id. To
handle all cases you may need to implement multistep process:
ObjectId oid = ....
// check if the object is cached
DataObject object = dataContext.getObjectStore().getObject(oid);
// fetch if not cached
if(object == null) {
SelectQuery sel = QueryUtils.selectObjectForId(oid);
List results = this.performQuery(sel);
if(results.size() == 1) {
object = (DataObject)results.get(0);
}
}
// create new if does not exist...
// ObjectId columns must be mapped to class attributes for this to work
if(object == null) {
object = dataContext.createAndRegisterNewObject("someEntity");
object.setId(id);
.....
}
Andrus
On Nov 7, 2003, at 4:13 PM, Bret Gregory wrote:
> I think I found another problem with creating the ID from the FF.
>
> Basically, I am creating the object using
> DataContext.registeredObject(...)
> like you suggested. Then I am updating the object by setting the
> various
> obj properties. This process seems to take the Obj and attempt to
> refetch
> it.
>
> Since it doesn't find anything in the DB (everything is in memory), it
> puts
> the object in a TRANSIENT State. If I change the state to NEW I get a
> ClassCastException. If I try to put it in MODIFIED state I get a
> CayenneRuntimeException.
>
>
> -----Original Message-----
> From: Andrus Adamchik [mailto:andru..bjectstyle.org]
> Sent: Friday, November 07, 2003 11:05 AM
> To: cayenne-use..bjectstyle.org
> Subject: Re: Searching Object Cache
>
> Hi Bret,
>
> If I understand your description correctly, what you need to do is to
> manually create an ObjectId from the flat file data for each Obj1 and
> call "DataContext.registeredObject(..)":
>
> http://objectstyle.org/cayenne/api/cayenne/org/objectstyle/cayenne/
> access/
> DataContext.html#registeredObject(org.objectstyle.cayenne.ObjectId)
>
> E.g.
>
> int id = ... // read from file
> ObjectId id = new ObjectId(Obj1.class, "idName", id);
> Obj1 object = (Obj1) dataContext.registeredObject(id);
>
> Andrus
>
>
> On Nov 7, 2003, at 10:22 AM, Bret Gregory wrote:
>
>> I know that this topic has been discussed before and I am aware that
>> Cayenne
>> is working as intended, but I am trying to figure out a way to look at
>> the
>> objects that cayenne has either previously created or fetched before
>> it
>> looks at whats in the DB.
>>
>> The scenario that I am running into is as follows:
>>
>> I am processing a flat file. On each line you have obj1 + obj2. obj1
>> has a
>> to-many relationship to obj2. The key for object 1 is not
>> auto-generated.
>>
>> For example:
>> Obj1-ABC Obj2-123
>> Obj1-ABC Obj2-345
>> Obj1-ABC Obj2-678
>> Etc.
>>
>> For each of these lines I want to fetch Obj1 and/or create it if
>> necessary.
>>
>> On each subsequent line I would like the Obj1 that I found/created to
>> be
>> returned.
>>
>> The problem is that if Obj1 was created and it is in memory then each
>> subsequent line will not find it in the DB and thus create a
>> duplicate.
>> This obviously causes a problem when you try to insert the data into
>> the DB
>> and the primary keys are duplicated.
>> I would prefer not to commit after the creation of each new Obj1 since
>> I
>> could have 1000s of Obj1's in the FF.
>>
>> So I am looking for a work around where this scenario would be
>> supported.
>> Thanks!
>>
>
This archive was generated by hypermail 2.0.0 : Fri Nov 07 2003 - 17:23:30 EST