Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Factoring Out Address Information

 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll bet this is a common question, but I couldn't figure out how to search for it...

Say I've factored out (street) "Address" from several classes in my OOD. Is it also wise to factor out Address (into its own table) in the RDB? I'm fairly new to this stuff, and I don't see not, but I've had Address unfactored in past (procedural) applications, and the switch seems weird.

Out of curiosity, does Hibernate support both RDB implementations (Address-as-a-separate-entity vs. Address-as-a-set-of-fields-within-other-entities) for the "factored Address" OO class model?

Thanks,
Jamie
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
if i understand your question correct, then you are looking for something hibernate calls Component.



pascal
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, a component is what you want. And you should be able to have it in the same table or another table with a one-to-one relationship.
 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's what I'm after, thanks.

In the case of, say, Address, is there any prevailing preference (entity vs. component)? I'm leaning toward a separate entity, but one-to-one relationships seem odd to me. (Is that something I just need to get over?)

Thanks again,
Jamie
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that the Hibernate model doesn't have to match the database model. If you put the address into its own table, the database has a one-to-one relationship. But in Hibernate you'd just embed the component directly into its owner. To load the address, you'd have to load its owner and do getAddress().

Even if you allowed for multiple addresses per owner, I believe you can still model it as a set of components without a one-to-many relationship. I'm not positive there as I haven't used components yet. All my objects are entities as I ported an EJB application (with entity beans) straight over and was given no time to experiment unfortunately.

So I think you only need to create a separate entity if you want to be able to load addresses directly. Again, this is new to me so I'd read up on the docs and perhaps hope one of the more experienced Hibernators chimes in.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic