• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OneToMany Best Practice

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a small project that I've completed many times in many different languages and many different frameworks. It's kind of my own Java Pet Store if you will. I'm redoing my DAO layer to use JPA and I have a question on OneToMany. A bit of a description.

ProductType.java - This contains types of products which might be Print, Bookmark, or Note Card.


Piece.java - A Piece is like a work of art. So this might contain something like "Cautious Cougar" or "Majestic". This is typically the original piece that gets turned into the different ProductTypes.


Product.java - Product is simply the sellable product. In the past I have called this ProductDetails but I trimmed it down. So as you can see this contains a Piece, a ProductType, size, price, etc.


So, finally, my question now is with respect to the association between Piece and Product. What I do is I display the Piece as an image. Clicking the image then takes me to that Piece's Products. And like I said, the way I have it now works. What I would like to know is if this is the best/preferred/optimal way of handling this type of association.

Thanks.
 
Shailesh Kini
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg,

In my opinion you are going in the right direction. It does work well when the "OneToMany" relationship is actually "OneToFew". Considering a simple scenario when a Peice has lot of Products associated with it, it's advisible to use some kind of paging mechanism to display the records. However, OneToMany does not provide any support for paging, it will end up retrieving all the associated records from the DB which may not be appropriate.

[ August 17, 2007: Message edited by: Shailesh Kini ]
[ August 17, 2007: Message edited by: Shailesh Kini ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks perfectly fine to me. Lazy loading is the default, so you don't have to put that in there.

I highly recommend everyone to always map every relationship as lazy, except maybe ManyToOne or OneToOne (Really is ManyToOne in database anyway), just because you can get the data relatively cheaply in that single query. I say all lazy because it is really each individual use case in your application that should determine the fetching strategy at time of creating query objects. Therefore each use case just gets the exact amount of data it needs to complete its work and nothing more or less than that. (There are always those rare occasions that might not make it "every", but they are very rare, such that I can't think of any)

Caching and pagination should also be considered based on your Use Cases and which objects would need that data cached.

Mark
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to hear. Thanks all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic