• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does book suggest best practices?

 
Varun Chopra
Ranch Hand
Posts: 211
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Chrome Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the author talks a little bit about this in the "Advanced Topics" section. I would also be interested his opinion
 
Mike Keith
author
Ranch Hand
Posts: 304
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Varun Chopra
Ranch Hand
Posts: 211
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 304
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Keith
author
Ranch Hand
Posts: 304
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hah! I actually know of more than one book that seems to have (unwittingly) done exactly that...
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Chrome Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33689
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Varun Chopra
Ranch Hand
Posts: 211
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33689
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spritzler wrote:And now the fun ended. ;)

Fun killer me. Imagine that.
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 304
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic