Re: Is "that" difficult to make an Update operation?
Re: Is "that" difficult to make an Update operation?
- Subject: Re: Is "that" difficult to make an Update operation?
- From: Gustavo Pizano <email@hidden>
- Date: Sun, 1 Mar 2009 12:46:09 +0100
Hello Mike.
public static Product findByProductCode(EOEditingContext ec,
String productCode)
...and you would do like...
Product thisProduct =
Product.findByProductUPC(defaultEditingContext(), "somecode");
if (thisProduct != null) {
thisProduct.addQuantity( 5 ); // or whatever quantity to be
added/subtracted, etc, which in turn would use the built-in method
setQuantity(Integer value);
}
defaultEditingContext().saveChanges(); // depending on your
specifics, maybe this should be inside the 'if' statement above?
MM are you telling me that EOGenerated java file has such a methods,
or that I should implemented in the subclass of _Product.java?
If they should be, then there are not, in my case, :S:S :(.
Well yes, Im new to EOF, have been trying to learn from a while, but
always something happens and I have to put it aside, so so far, to
keep things simple at the beginning Im using the session's
defaultEditingContext, probably I will change it for some other
operation to a custom ERXEC.
#2. The rest of your code in regards to actually grabbing the
EOModel, grabbing the EODatabaseContext, the EODatabaseChannel, etc
- yes, you are 100% correct, it is generally that convoluted. The
point of EOF is to abstract the relational database away so it is
very infrequent that you should ever have to do this. With EOF, you
want to think in terms of the 'object graph' - java objects in your
editing context.
About the adaptors and dbcontexts, when you refer to , "you should
ever have to do this", its because of the methods you mentioned at the
beginning of your reply? which would handle this situation for me?,
but as I said I have no such a methods generated.
Oh, I guess P.P.S. - make sure you are using the project wonder
frameworks. All that craziness to get the EODatabaseChannel, even
though you rarely need it, has cover methods in Wonder, and there
are multitudes and multitudes of other things like that in Wonder too.
Yes Im using wonder, so where , or how can I use it to my advantage
with the EOChannel's ?, I was looking a way but couldn't .
Thanks for your reply
Gus
On 1.3.2009, at 10:51, Michael DeMan (WO) wrote:
Hi Gustavo,
Two things I can think of, feel free to elaborate more since I am
responding from an overall quick read of what you are doing and
presuming you are pretty new to EOF. My response here could be
completely off base if my instinct your questions is not correct.
In regards to your one question about getting a hold of
There are two things going here that I can think of.
#1. The fundamental design of EOF is to avoid doing things like
talking to database adaptors, JDBC/ODBC clients, etc, directly. For
the section of code where you need to update the quantities,
normally, for a given database table like 'PRODUCT', there would be
an associated java EO object, with a utility method like...
public static Product findByProductCode(EOEditingContext ec,
String productCode)
...and you would do like...
Product thisProduct =
Product.findByProductUPC(defaultEditingContext(), "somecode");
if (thisProduct != null) {
thisProduct.addQuantity( 5 ); // or whatever quantity to be
added/subtracted, etc, which in turn would use the built-in method
setQuantity(Integer value);
}
defaultEditingContext().saveChanges(); // depending on your
specifics, maybe this should be inside the 'if' statement above?
...keep in mind the above is just very loose pseudo-code as an
example, and using the 'defaultEditingContext()' is just a quick
convenience for the pseudo-code. Also keep in mind that the static
utility method is something you will either need to make by hand, or
can have made automatically by EOGenerator.
#2. The rest of your code in regards to actually grabbing the
EOModel, grabbing the EODatabaseContext, the EODatabaseChannel, etc
- yes, you are 100% correct, it is generally that convoluted. The
point of EOF is to abstract the relational database away so it is
very infrequent that you should ever have to do this. With EOF, you
want to think in terms of the 'object graph' - java objects in your
editing context.
It requires a slight shift of thinking to use EOF effectively, and
even though we as java programmers want to 100% work with graphs of
persistent object graphs in a fluid and simple way, there is the old
impedence mismatch between object graphs and traditional SQL
databases.
http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch
In regards to those 'static utility methods' that you should have in
every java EO class to simplify the code needed to fetch things,
create new objects, etc, from the database:
Make sure you have an actual 'hard' java class for each one of your
enterprise objects. The best way to do it when first getting
started, is to use EOGenerator...
http://en.wikibooks.org/wiki/WebObjects/EOF/Using_EOF/EOGenerator
...and it will make a bunch of them for you automatically.
Note that with EOGenerator, you will want to end up with two classes
generated from each EO, like...
com.mycompany.eo.core.Product
com.mycompany.eo.Product
The latter being a subclass of the prior. When you add additional
custom business logic, you will do it to com.mycompany.eo.Product,
and if you change your EOModel around and run EOGenerator again, it
will update all the com.mycompany.eo.core classes for you, leaving
your custom business logic untouched.
Don't know if this helps or not.
Oh, I guess P.P.S. - make sure you are using the project wonder
frameworks. All that craziness to get the EODatabaseChannel, even
though you rarely need it, has cover methods in Wonder, and there
are multitudes and multitudes of other things like that in Wonder too.
- mike
Finally, out of it all, maybe I am 100% off base on understanding
the question you are asking. To me, looks like somebody new to EOF
thinking to use more traditional mechanisms to access the database.
Could be you
Hello. Ok the background. I made a fetch from of a "Product" on
the database like this:
try {
//session().defaultEditingContext().saveChanges();
Product p =
(Product
)EOUtilities
.objectMatchingKeyAndValue(session().defaultEditingContext(),
"Product", Product.CODE_KEY, code);
//If I get thi this line its because the producto already exists so
I will inform the user
return this.informUser(p);
} catch (EOObjectNotAvailableException e) {
// TODO Auto-generated catch block
//If I came into the catch is because the product does not exist so
it's save to saveCahnges();
now, as you can see if the product exists, I will inform the user,
but I decided to upgrade the number of itmes at the same time, so
I did the following, based on an Apple support page:
/**
* Method that will update the old quantity with the new one, in
case the Procut already exists
* @param p
* @return
*/
private WOActionResults informUser(Product p) {
EOAdaptor adaptor;
EOAdaptorContext adContext;
EOAdaptorChannel adChannel;
EOFetchSpecification fspec =new
EOFetchSpecification(Product.ENTITY_NAME, null, null);
EOObjectStoreCoordinator rootStore = (EOObjectStoreCoordinator)
session().defaultEditingContext().rootObjectStore();
EODatabaseContext dbContext = (EODatabaseContext)
rootStore.objectStoreForFetchSpecification(fspec);
EODatabaseChannel dbChannel = dbContext.availableChannel();
adaptor = dbContext.database().adaptor();
adContext = dbContext.adaptorContext();
adChannel = dbChannel.adaptorChannel();
NSDictionary dic = new NSDictionary((new
Integer(Integer.parseInt(itemQty)))
+p.numberItemsInStock(),Product.NUMBER_ITEMS_IN_STOCK_KEY);
EOQualifier qual = Product.CODE.eq(code);
EOEntity myEntity =
(EOModelGroup
.defaultGroup
().modelNamed("StockManager")).entityNamed(Product.ENTITY_NAME);
System.out.println("Opening Channel");
adChannel.openChannel();
adChannel.updateValuesInRowDescribedByQualifier(dic, qual, myEntity);
System.out.println("Closing Channel");
adChannel.closeChannel();
// TODO Auto-generated method stub
errorMessage = "The product with Code Number: "+ code + ", already
exists in the DataBase, the Quantity has been updated it ";
AjaxHighlight.highlight("errMess");
return pageWithName("RegisterProducts");
}
Then I tested and wow, it worked then I read it, and WOW, got
confused, I had to back to the EO PRogramming guide and read again
what are those EOAdaptor things, and it was quite confusing. Then
what I did, was that once the first time I updated the quantity, I
just immediately save the the registration again, to see what would
happen, the app told me that eventually the number of Items in
stock were updated, I checked the db again, but for my surprise
this time was reduced.
Ok questions.: is that the right method to make an update in a a
db?. is there another way to get the EOModel, so I can get the
Entity, and then use the method from EOAdapterChannel
updateValuesinRowDescribedByQualifiers?, and, why it might happened
that after the first update, if I click once again the submit
button, the numbers of items were reduced?
Well if any better explanation of what I did here that will help me
clear my mental map, I will appreciate it also.
Thanks
Gus
------------------------------------------------------------------------
_______________________________________________
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
_______________________________________________
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
_______________________________________________
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