• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Hibernate Many-to-many using link table

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you send the code for the mapping files for these classes ?
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic