My question to you is does your new book suggest good practices for better maintenance and performance while using JPA2?
I have seen a lot of JPA code that uses JPA queries while the same might be coded using objects only without those where clauses. So does the book draw a line to suggest when to go for queries and when to attempt to achieve results with objects only, and what is the pros and cons of using queries in JPA?
Well, I wouldn't be very happy with a book that was supposed to be written an an expert in the field but didn't offer some best practices and performance tips ;)
Queries are almost always the way to go. They return exactly the objects that you want, when you want them, without having to navigate through and load other unnecessary objects along the way. If the objects already happen to be in the persistence context then you might be better off just navigating to the objects without doing any db selects, but named queries will typically be your best bet as long as you have your flush mode set to COMMIT and you are not intending to pick up changed objects in your transaction.
Thanks Mike for your reply. I guess it makes sense.
At the same time I am also concerned about overuse of queries over object navigation. Like user.getAddress().getCity() is coded by some programmers as a query to address and user tables with "where userid=?". This gives me feeling of "going back to jdbc style" instead of object oriented code. So for performance reason it may make sense but not in general. What do you think?
Mike Keith
author
Ranch Hand
Joined: Jul 14, 2005
Posts: 304
posted
0
A simple navigation along single-valued relationships is likely going to be faster in memory because the objects are probably already loaded into the cache/persistence context. This is less common, though. You normally hit a collection fairly soon. Navigating down a single level is fine and typical, going down more levels usually becomes better to use a query (unless you actually want to access all of the intermediate objects as well, of course).
Mark Spritzler wrote:I think I will write a book and make sure it has all the worst practices in the world, but make people think it is the way to do it.
Sorry, I woke up really well this morning, but with some sarcasm in my coffee.
Mark
Mark, I am eager to see your book appear on JavaRanch Book Promotions
Mark Spritzler wrote:I think I will write a book and make sure it has all the worst practices in the world, but make people think it is the way to do it.
Sorry, I woke up really well this morning, but with some sarcasm in my coffee.
Mark
Mark, I am eager to see your book appear on JavaRanch Book Promotions
I am sure it would be in the Meaningless Drivel forum.
Mark Spritzler wrote:
I am sure it would be in the Meaningless Drivel forum.
Which is where this thread is starting to belong.
Note to all: I'm letting this thread stay because it is joking and involves the authors this week. None of the later posts are eligible to win in the promo of course. (The posters are for their other posts; just mentioning so people don't think chatter counts.)
Mike Keith wrote:A simple navigation along single-valued relationships is likely going to be faster in memory because the objects are probably already loaded into the cache/persistence context. This is less common, though. You normally hit a collection fairly soon. Navigating down a single level is fine and typical, going down more levels usually becomes better to use a query (unless you actually want to access all of the intermediate objects as well, of course).
Mark Spritzler wrote:
I am sure it would be in the Meaningless Drivel forum.
Which is where this thread is starting to belong.
Note to all: I'm letting this thread stay because it is joking and involves the authors this week. None of the later posts are eligible to win in the promo of course. (The posters are for their other posts; just mentioning so people don't think chatter counts.)