File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Does book suggest best practices? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Does book suggest best practices?" Watch "Does book suggest best practices?" New topic
Author

Does book suggest best practices?

Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
Hi Mike and Merrick

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?

regards
Varun


-Varun -
(My Blog) - Online Certifications - Webner Solutions
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

I think the author talks a little bit about this in the "Advanced Topics" section. I would also be interested his opinion


===Vyas Sanzgiri===
My Blog
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
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.


-Mike
Pro JPA 2: Mastering the Java Persistence API
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
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
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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Hah! I actually know of more than one book that seems to have (unwittingly) done exactly that...
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Vyas Sanzgiri wrote:
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
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29240
    
139

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.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
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).


Thanks for your expert comments Mike.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Jeanne Boyarsky wrote:
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.)


And now the fun ended. ;)

btw. I am not eligible to win any book.

Mark
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29240
    
139

Mark Spritzler wrote:And now the fun ended. ;)

Fun killer me. Imagine that.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Jeanne Boyarsky wrote:
Mark Spritzler wrote:And now the fun ended. ;)

Fun killer me. Imagine that.


FYI, outside of the forums, in real life. Jeanne is a lot of fun to be around.

Mark
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Wha, this forum isn't real life?
Now I'm wondering if I should take off these six-shooters I've been wearing around the house.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Mike Keith wrote:Wha, this forum isn't real life?
Now I'm wondering if I should take off these six-shooters I've been wearing around the house.


Now you have to post pictures.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does book suggest best practices?
 
Similar Threads
Hibernate and JPA with Spring
Hibernate Performance Tuning
Patterns & JPA in Hibernate Recipes
efficient way to perform CRUD operation in Oracle TopLink
Apache Struts 2 Web Application Development - What's new?