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: Chuck Hill <email@hidden>
- Date: Tue, 15 Mar 2005 11:49:38 -0800
On Mar 15, 2005, at 10:17 AM, Baiss Eric Magnusson wrote:
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 );
To start with, that looks wrong. createInstanceWithEditingContext
should, I expect, insert the object for you. I would prefer to
simplify this code into just:
Event event = (Event) EOUtilties,createAndInsertInstance(ec, "Event");
One short line trumps three. :-)
...
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.
That seems most odd. Owns Destination objects should be created for
you. Have you tried to log out ec.insertedObjects()? Is the
relationship marked as Mandatory in the EOModel?
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.
Yes, the docs are not the clearest. But this is not only for delete
rules. It also controls (or should control!) object creation. This is
a frequent pitfall as people go on to create an object that EOF has
already created and there end up being two in the EC, one hooked to the
manually created object and one not related to anything.
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.
That too should not be a problem. EOF should be creating all the
primary keys before starting to insert rows. You can run into a
problem when the foreign key for a row being inserted refers to a row
not yet inserted into another table. Marking the FK constraints as
DEFERRABLE INITIALLY DEFERRED avoids this.
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)).
No, something is going grievously astray there.
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
That comes from relating an object that is in an EC to one that is not
in an EC or, perhaps, relating one new object to another and then
deleting one of the objects before saving.
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.
And one that should be unnecessary. Try changing how you create the
objects (from top of message) and see if that has any benefit.
Chuck
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:
village.net
This email sent to email@hidden
--
Practical WebObjects - a book for intermediate WebObjects developers
who want to increase their overall knowledge of WebObjects, or those
who are trying to solve specific application development problems.
http://www.global-village.net/products/practical_webobjects
_______________________________________________
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