Re: EditingContext problem, ghost entry in datastore [ was Debuging WOManyToManyRelationship]
Re: EditingContext problem, ghost entry in datastore [ was Debuging WOManyToManyRelationship]
- Subject: Re: EditingContext problem, ghost entry in datastore [ was Debuging WOManyToManyRelationship]
- From: Dev WO <email@hidden>
- Date: Sat, 23 Jul 2005 17:50:34 +0200
Thanks Chuck for all the pointer and suggestions, I'll try that in a
minute.
Le 23 juil. 05 à 17:12, Chuck Hill a écrit :
WARNING: Half a cup of coffee into my morning... :-)
I'd guess that the problem you are seeing only occurs when editing
an existing object. Test that and see if is the case.
In fact the problem occurs at first run of the component, either for
adding one or updating one. In fact an object from the constructor
(one with salesPrice 52) is added to the datastore...
I'll try that and come back in a few minutes...Thanks:)
Not too bad for a half cup of coffe;)
You insert a new object in your constructor and then set
productGeneric to be object to be edited. This still leaves the
one you created in the editing context. One way to work around
this is to call revert on the editing context when the method to
edit an object is called. This will discard the newly created object.
However, I will suggest some slightly better design. My preference
in writing code is to only refer to instance variables directly in
the methods that set and return them. I sometimes make an
exception for the constructor if doing so makes the code clearer.
This is much better for encapsulation and is nearly crucial when
subclassing.
I will suggest that you add this method to your page:
public ProductGeneric productGeneric() {
// Create a new object is one is requested and not yet set
if (productGeneric == null) {
productGeneric = (ProductGeneric)
EOUtilities.createAndInsertInstance(session.defaultEditingContext
(), "ProductGeneric");
productGeneric.setCommentaryAllowed(new Boolean(true));
productGeneric.setIsAvailable(new Boolean(true));
// This is to help me find which object is generated
productGeneric.setSalesPrice(new Integer(52));
}
return productGeneric;
}
Then use productGeneric() instead of productGeneric in your code.
If you don't have this method, I'd add it (feel free to change the
name):
public void setProductToBeEdited(ProductGeneric product) {
session.defaultEditingContext().revert();
productGeneric = product;
}
More changes below.
On Jul 23, 2005, at 7:41 AM, Dev WO wrote:
So finally the real problem is the editing context.
What I have is some weird behavior when adding/updating my object:
some extra objects are saved in the datastore!
here's my application constructor:
-----
public ProductsPage(WOContext context) {
super(context);
session = (Session)session();
ec = session.defaultEditingContext();
Remove these lines, this work in now done by productGeneric():
productGeneric = (ProductGeneric)
EOUtilities.createAndInsertInstance(ec, "ProductGeneric");
productGeneric.setCommentaryAllowed(new Boolean(true));
productGeneric.setIsAvailable(new Boolean(true));
// This is to help me find which object is generated
productGeneric.setSalesPrice(new Integer(52));
}
-----
So if I understand correctly, I'd say that I have a productGeneric
Object which is inserted in the ec (defaultEC), with a couple
default value to be able to "see" where's coming from the ghost
object.
In my addOrUpdate method, I have:
-----
public WOComponent addOrUpdateProductGeneric()
{
awakeFromInsertionProductGeneric(ec); //setting the date
posted/updated
if (!productGenericList.containsObject(productGeneric))
{
productGeneric.setStockStatus(new Integer(0));
}
else
{
editMode = false;
}
ec.saveChanges();
Replace the code below with something like:
// We just created a new object and saved it.
// Reset page so that another new object will be created
if (editMode) {
setProductToBeEdited(null);
}
productGeneric = new ProductGeneric();
ec.insertObject(productGeneric);
productGeneric.setCommentaryAllowed(new Boolean(true));
productGeneric.setIsAvailable(new Boolean(true));
productGeneric.setSalesPrice(new Integer(0));
return context().page();
}
Chuck
-----
but if I add a new product, I got another one inserted to the
datastore as well. The ghost comes from the component constructor
(because it has the salesPrice set to 52)...
I most probably do it the wrong way for sure (if not it would
work), but I just can't find where I do it wrong...
The method really look like:
-it gives some extra values to the productGeneric object (like
datePosted trough the custom awakeFromInsertionProductGeneric)
-then save the ec (which should only have this object)
-and finally create an empty productGeneric, insert it in the ec
and set a couple values (which works fine because there's no
problem after the first saving)
Did I read my own code incorrectly? probably but I really can't
figure this out...
any tip that could help me is more than welcome.
Xavier
PS: now I know I have to put the object into and editing context
before manipulating it, but pretty much all the resources I
learned from (Apple's documentation, WebObjects5 book, ...) gave
me a bad habit;)
--
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