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 using link table

 
joe priv
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just inherited a project where none of the original developers are available. It is half baked and I was hired to finish it. With that said, I am looking for a couple of things:

First, for a simple working example of how to map a unidirectional
many-to-many relationship using a link table in-between preferably using struts.

Second, some specific clues on how to accomplish the following (we are using struts and hibernate):

The link (in between) table does not use a composite key, but a unique ID (sequence) with extra columns for more data. The code that is already there (but not working) is using 2 one-to-many maps to/from the link table. I can't find good information on why you would use 2 one-to-many's in place of a true many-to-many (eg hibernate tutorial). Also, what is the best way to update an existing relationship? Example user clicks a link, which retrieves data, a form is populated, user makes changes, action is submitted; now we need to update this info. We are extracting info from the actionform and mapping it back to Hibernate objects (it this best way to do that?). Now we need to update this using I am guessing saveorupdate() and flush(). I know this is probably too much to ask, but any help or where I can find examples would be great. Thanks in advance.
 
anoumou vossah
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you send the code for the mapping files for these classes ?
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ask two main questions (and some additional ones):

Q1. How to do a many-to-many using a join table?
A1. There is really no way to do a many-to-many WITHOUT a join table. Follow the hibernate documentation on how to do this, it is almost painfully exhaustive. Also, this has very little to do with Struts, it's all about your database design and ORM, handled by hibernate.

Q2. How to do a many-to-many with a link table that contains some data in excess of what is needed to maintain the many-to-many relationship?
A2. In the database you simply add the extra columns to the table. In the Java/Hibernate world, you simply create another object, which holds a reference to both sides and the additional info. For example, consider a relationship among Persons and Cars. Each person can have many cars and each car can have many owners. In addition, you want to track the dates for when each person owned what car. You simply put the startDate and endDate columns into the join table and then you create an extra object, like PersonCar, which contains a Person, a Car, a startDate and an endDate.

Additional answers:

1. I am not sure what is the benefit of using 2 one-to-many relationships to model a many-to-many relationship.
2. You can use saveOrUpdate to update existing data. You should have autoflush enabled so you should not need to call it explicitly.

Hope this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic