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

Hibernate: many-to-many object mappings

 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all:

I am just getting started with Hibernate 3.2. I want to know what are the advantages and the disadvantages of enabling many-to-many relationship between objects?

For example: If I want to map a relational database where the tables are conneceted to each other by a foriegn key. Using Hibernate, one have the option to make the bean reference each other -i.e inheretance, composition- or can make each bean represent one table only without reference to the other tables.

One advantage of the later approach the I can think of is that faster query, and small memory foot print, because I can retrieve one object only if I want to.

The disadvantage that I can think of is that I will have to make more than one query to retrieve a all the data if I need to.

Can you share your knowledge and experience with me.

Thanks
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in your example, you seem to be showing a One To Many relationship. a ManyToMany would have a join table between two tables.

The purpose of Hiberate is so that you can create a Java Object Model and follow OO Best Practices. Now there is a mismatch between Data Modeling and Object Modeling that requires a middleman to do the conversion, mapping, etc.

I would not worry about the data loading and inserting just yet. I would first build a really good Object Model, then see how to map that to the third normal form data model that your DBA creates. Then you map to that.

For queries, yes there is a big difference between loading 100 objects with just one query over 100 seperate queries for each one, you have to be careful of network traffic, how many times you try to connect to the database and how much data you are bringing over the wire when you do query the database. In Hibernate, you can have full control over each and every query you run. But, you have to go through the learning curve and learn how to do that.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic