Reputation: 45722
I'm readign the book "Java Persistance with Hibernate" and I faced with example of one-2-one using join table (second edition, page. 230). But I still can't understand the advantages of such approach.
Can you explain me them? Is it optional one-2-one assosiation. In such case. why can't I simply use nullable Foreign-Key in one of the assosiating tables?
Upvotes: 0
Views: 1243
Reputation: 2027
It is optional to use a JoinTable in an OneToMany relationship. I did not read this book but I can think in at least two possible advantages of using JoinTable in a OneToOne relationship:
Easy to migrate to a OneToMany or ManyToMany: let's say that you have an entity Person OneToOne Address, all works fine for years. However, now you have a new business rule that says that Person ManyToMany Address, so you have to write scripts to migrate data and change your table structure. If you have a JoinTable you just need to change the relationship.
You can delete records from the JoinTable instead of the Entity table: in some scenarios this may be an advantage. Again using our Person OneToOne Address, if you want to delete the Address of a Person you can just delete it from the relationship.
Cheers,
Upvotes: 3