Re: Why can't I get <addObjectToBothSidesOfRelationshipWithKey> right?
Re: Why can't I get <addObjectToBothSidesOfRelationshipWithKey> right?
- Subject: Re: Why can't I get <addObjectToBothSidesOfRelationshipWithKey> right?
- From: Baiss Eric Magnusson <email@hidden>
- Date: Tue, 15 Mar 2005 10:17:34 -0800
On Mar 14, 2005, at 4:26 PM, Arturo Pérez wrote:
When you set a relationship to be "Owns Destination" EOF automatically
creates one for you. So, you already have a Task. When you make a
new one and add it you end up with one floating around uninitialized.
Your code should be
In the WOComponent
EOClassDescription cd =
EOClassDescription.classDescriptionForEntityName( "Event" );
Event event = (Event)cd.createInstanceWithEditingContext( ec, null );
ec.insertObject( event );
...
Task t = event.task();
// set up task properly.
ec.saveChanges();
-arturo
I don't believe this is correct, I have been dumping the <event.task()>
in many possible cases and it is always null. It doesn't matter if I
<Owns Destination> or not. By pouring over the <Owns Destination>
documentation (EOModeler is poorly documented, IMO), it is simply a
mechanism for maintaining delete rules.
My problem with <addObjectToBothSidesOfRelationshipWithKey>,
throughout the ages, and I do mean years, is that when the
<ec.saveChanges()> occurs the primary keys are assigned and when you
are trying to work with 2 related objects simultaneously the
relationship fails because the DB hasn't assigned the primary key for
the 2nd object. The primary keys are always assigned at
ec.saveChanges() time and no other time. I can't seem to get the
staging correct.
So I get:
Next exception:SQL State:23 -- error code: 359 -- msg: Exception
condition 359. Integrity constraint violation (NULL in PRIMARY KEY,
TASK._C0000000034(EVENT_ID=NULL)).
Or, if I don't <insertObject I get:
[IllegalStateException] Cannot obtain globalId for an object which is
not registered in any editingContext, object: {values = {...toEvent =
"<Event 52a84b <EOTemporaryGlobalID: 0 0 -64 -88 1 101 0 0 -39 -27 1 0
0 0 1 2 -89 37 -77 50 78 -61 52 51>>"; }; this = "<Task 27868b [Gid
Not Found]>"; }, databaseContext:
com.webobjects.eoaccess.EODatabaseContext@a0fbd6, object's
editingContext: null, databaseContext's active editingContext:
com.webobjects.eocontrol.EOEditingContext@fa19ca: <doAddEvent> failed
trying to enter a new Event
Sheepishly I have always hacked it by first doing a ec.saveChanges() on
the parent and then a ec.saveChanges() on the child, a terribly
annoying hack.
On Mar 14, 2005, at 6:53 PM, Baiss Eric Magnusson wrote:
I have an Event record which wants to have a Task record, it is to be
a one-to-one relationship.
I set the relationship delete rules from the Event record to the Task
record to be:
Cascade
Owns Destination
Propagate Primary Key
From the Task record to the Event record I set relationship delete
rules to be:
Nullify
In the model, I define the primary key for the Task record to be
<eventID>, which is the same name as the primary key of the Event
record. I don't set the primary key attribute for the Task table in
FB, because it won't let me.
At <doAddEvent> time, (both records are created at once):
In Event, which overrides EOGenericRecord
public Task toTask() {
return (Task)storedValueForKey("toTask");
}
In the WOComponent
EOClassDescription cd =
EOClassDescription.classDescriptionForEntityName( "Event" );
Event event = (Event)cd.createInstanceWithEditingContext( ec, null );
ec.insertObject( event );
...
EOClassDescription cd =
EOClassDescription.classDescriptionForEntityName( "Task" );
Task newTask = (Task)cd.createInstanceWithEditingContext( ec, null );
ec.insertObject( newTask );
...
event.addObjectToBothSidesOfRelationshipWithKey( newTask, "toTask");
ec.saveChanges();
Which causes:
WOComponentRequestHandler>: Exception occurred while handling request:
NSForwardException [java.lang.IllegalArgumentException] Attempt to
insert null object into an NSMutableDictionary:
<doAddEvent> failed trying to enter a new Event
In FB, I define in Task the foreign key
foreign key ("EVENT_ID") references "EVENT" ("EVENT_ID") match full
on delete set null deferrable initially deferred
----
Baiss Eric Magnusson
<http://www.Track-Your-Finances.com>
<http://www.CascadeWebDesign.com>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden