Re: Debuging WOManyToManyRelationship
Re: Debuging WOManyToManyRelationship
- Subject: Re: Debuging WOManyToManyRelationship
- From: Chuck Hill <email@hidden>
- Date: Fri, 22 Jul 2005 14:58:42 -0700
Hi Xavier,
First off, the source code to WOToManyRelationship is on your drive.
See
/Developer/Examples/JavaWebObjects/Source/JavaWOExtensions/
WOToManyRelationship.java
On Jul 22, 2005, at 11:27 AM, Dev WO wrote:
After digging for hours, I have found something very strange:
If I remove the WOToMany, I can add object to the database, then
I put back the WOToMany, then edit a previous object (already in
the DB) then the WOToMany works, everything gets written in the
join table, no problem...
So I could save all products first then apply them to the
categories (WOToMany), but in fact I can't do that as I got the
crash even if I don't select anything in the WOToMany browser...
So I understand this seems to be related to the object not inside
an EC when it is created. So I tried:
---
public WOComponent addOrUpdateProductGeneric()
{
if (!productGenericList.containsObject(productGeneric))
{
ec.insertObject(productGeneric);
This looks like you are editing an object not yet inserted into
the EC. Apple examples or not, this is bad practice.
I finally realize that, but how do I insert the first one, when the
component is first generated?
I've got
productGeneric = new ProductGeneric();
in the component constructor, do I have to do something else to
insert the object in the EC?
What is the ec? The session().defaultEditingContext()? IMHO the
best way to create most EO object (I'm sure there must be exceptions,
but I can't think of them) is to use:
productGeneric = (ProductGeneric)
EOUtilities.createAndInsertInstance(ec, "ProductGeneric");
productGenericList.addObject(productGeneric);
awakeFromInsertionProductGeneric(ec); // this is to
set the date posted
You should never call this yourself. Calling ec.insertObject
(productGeneric) calls this (unless you have forgotten to call
super.awakeFromInsertion() somewhere.
I've got multiple awakeFromInsertionXXX method in this component as
there's multiple object that can be added/edited in this component
(hidden by conditionals and an internal menu). If I use
ec.insertObject(productGeneric) instead, I have the feeling I'll
get some trouble with other object... am I wrong?
That should not cause a problem.
or should I try to "detect" what kind of object I'm manipulating
(productGeneric, productCustom1, ect)?
Your awakeFromInsertionProductGeneric method is fine, I just sleepily
mistook it for productGeneric.awakeFromInsertion(ec);
Chuck
}
else
{
awakeFromInsertionProductGeneric(ec); // this is to
set the date last updated
definitely a bad thing to do. See the CooperatingEditingContext
in Practical WebObjects for a good way to do this.
the awakeFromInsertionXXX method is really simple, it just checks
if the item has a datePosted, if it's not the case, he's applying
one, and if it's the case, it applies an dateUpdated.
Only one person is going to edit/add stuff in there, so I "think" I
don't have the need for something complex if I can do it "the
simple way"... or should I say that I'm running out of time for
this psecific component as my customer must be able to fill the
database in a couple of hours:(
Sorry, my bad. I had just gotten out of bed and did not read past
"awakeFromInsertion" to see that this was a custom method of yours
and not the EOEnterpriseObject standard one. I really should drink
more coffee before trying to read e-mail. :-)
What you are doing here is OK.
}
productGeneric = new ProductGeneric();
ec.saveChanges();
return context().page();
---
But it leads to the same crash...
any ideas?
Thanks
Le 22 juil. 05 à 13:01, Dev WO a écrit :
I've been setting up a form with a WOManyToMany relationship, I
tried it and it worked, the correct values were written in the
database in the join-table.
I still worked on this project, though I don't remember exactly
what I've done...
I had to left this for a couple days and when back on it, I've
seen that saving would lead to an exception like the one Amedeo
Mantica got on may 19th 2005:
----
[2005-07-22 10:24:51 CEST] <WorkerThread7>
java.lang.NullPointerException
at
com.webobjects.woextensions.WOToManyRelationship.updateSourceObject
(WOToManyRelationship.java:348)
at
com.webobjects.woextensions.WOToManyRelationship.setSelections
(WOToManyRelationship.java:421)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.webobjects.foundation.NSKeyValueCoding
$1.setMethodValue(NSKeyValueCoding.java:688)
at com.webobjects.foundation.NSKeyValueCoding
$_MethodBinding.setValueInObject(NSKeyValueCoding.java:1175)
at com.webobjects.foundation.NSKeyValueCoding
$DefaultImplementation.takeValueForKey(NSKeyValueCoding.java:1293)
at com.webobjects.appserver.WOComponent.takeValueForKey
(WOComponent.java:1550)
at com.webobjects.foundation.NSKeyValueCoding
$Utility.takeValueForKey(NSKeyValueCoding.java:519)
at com.webobjects.foundation.NSValidation
$DefaultImplementation.validateTakeValueForKeyPath
(NSValidation.java:733)
at
com.webobjects.appserver.WOComponent.validateTakeValueForKeyPath
(WOComponent.java:1273)
at
com.webobjects.appserver._private.WOKeyValueAssociation.setValue
(WOKeyValueAssociation.java:71)
at
com.webobjects.appserver._private.WOBrowser._fastTakeValuesFromRequ
est(WOBrowser.java:153)
at
com.webobjects.appserver._private.WOBrowser.takeValuesFromRequest
(WOBrowser.java:168)
at
com.webobjects.appserver._private.WODynamicGroup.takeChildrenValues
FromRequest(WODynamicGroup.java:81)
at
com.webobjects.appserver._private.WODynamicGroup.takeValuesFromRequ
est(WODynamicGroup.java:89)
at
com.webobjects.appserver._private.WOConditional.takeValuesFromReque
st(WOConditional.java:41)
at
com.webobjects.appserver._private.WODynamicGroup.takeChildrenValues
FromRequest(WODynamicGroup.java:81)
at
com.webobjects.appserver._private.WODynamicGroup.takeValuesFromRequ
est(WODynamicGroup.ja
----
But has the solution was to change the workflow, it doesn't help
me fix my problem...
So let's try to explain it correctly:
-I've got a regular form, and without the WOToMany it works like
a charm.
-When adding the WOToMany, the display is also correct, so the
bindings should be ok (I've rechecked numerous time, and if I
made a mistake I couldn't get a correct display), here's
the .woa declaration:
----
ToManyRelationship: WOToManyRelationship {
relationshipKey = "productCategorys";
sourceObject = productGeneric;
sourceEntityName = "ProductGeneric";
uiStyle = "browser";
destinationDisplayKey = "name";
}
----
-When submitting the form (which also save ec), I got the
crash...Here's my save method:
----
public WOComponent addOrUpdateProductGeneric()
{
if (!productGenericList.containsObject(productGeneric))
{
productGenericList.addObject(productGeneric);
ec.insertObject(productGeneric);
awakeFromInsertionProductGeneric(ec); // this is to
set the date posted
}
else
{
awakeFromInsertionProductGeneric(ec); // this is to
set the date last updated
}
productGeneric = new ProductGeneric();
ec.saveChanges();
return context().page();
}
----
I really don't know how to track what's happening...
If anyone could come up with a clue, that would be a real savior.
Thanks
Xavier
PS: As mentioned by Chuck in an answer to Amedeo's problem:
----
This is where the execption happens, last line:
public void updateSourceObject(NSArray newValues) {
// add new values to relationship, remove old values
Object aSourceObject = _localSourceObject();
boolean isDictionary = (aSourceObject instanceof
NSMutableDictionary);
NSMutableDictionary _dictionary = (isDictionary) ?
(NSMutableDictionary) aSourceObject : null;
EOEnterpriseObject _eo = (!isDictionary) ?
(EOEnterpriseObject) aSourceObject : null;
String masterKey = _localRelationshipKey();
NSMutableArray currentValues = (NSMutableArray)
NSKeyValueCoding.Utility.valueForKey(aSourceObject, masterKey);
int count = currentValues.count();
Thus, NSKeyValueCoding.Utility.valueForKey(aSourceObject,
masterKey) returns null. In other words,
sourceObject.valueForKey(masterKey) returns null. Check the
spelling of your bindings. It looks like you have misspelled
the tomany relation.
Chuck
----
I've checked all bindings, and I cannot find a mistake. In
addition to that, Could I get the WOToMany to display if there
were an error?
--
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