0

I need help with Hibernate and many-to-many relationship. Here is my sample schema on this link. It's simple enough so you can easily get the point. Concentration is on tables PERSON, ADDRESS and PERSON_ADDRESS.

Java model is here on this link. My questions are:

  • Is it totally wrong to have that class PersonAddress?
  • If I leave it like that what are the problems I could encounter with, at some later stage of development?
  • How can I optimize it?

Git repo with my sample code is here.

shx
  • 1,068
  • 1
  • 14
  • 30
  • You don't have a plain M-N relation. It's an M-N with extra attributes on the relation, hence introducing an intermediate class (as you have done) and having two 1-N relations is the correct thing to do. So why not actually use it? You have no "problem" in this post. –  Sep 19 '14 at 06:57
  • What if I want to have collection of addresses in Person and collection of persons in Address? Is there need to avoid `private Collection personAddressCollection`? – shx Sep 20 '14 at 19:12

1 Answers1

0

Class PersonAddress is called join table. Hibernate supports join table and you don't need additional effort to achive it. But I'm not sure if you can add additional properties into join table. Hiberante bidirectional association

For additional properties on your join table you should go with @Embeddable and @EmbeddedId annotations, but then most of the joining work you will do on your own, look at this answer, Hiberante additional properties in join table.

Community
  • 1
  • 1
Petar Butkovic
  • 1,820
  • 15
  • 14