Do I need to manually create relation tables in Hibernate?
If I have two tables,
Person
PhoneNumbers
As it has a one-to-many relationship, do I have to manually create a third table like person_phone
, or will Hibernate automatically do tha开发者_开发百科t?
If it is a one-to-many relation, you don't need a third table; PhoneNumbers
would include a foreign key to the Person
table.
Even though you don't need a third table, you can have a third table (for optimization purposes, for instance). In this case, you can map the relationship as usual, plus adding the @JoinTable annotation:
http://download.oracle.com/javaee/6/api/javax/persistence/JoinTable.html
Alternatively, you can find more information about it in the Hibernate documentation (see section 5.1.7.1. Using a foreign key or an association table):
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#d0e7541
Do I have to manually create a third table like
person_phone
?
No, you don't have to.
Consider the relationship. It's one to many. i.e. a person may have many phone numbers. How would you map it without Hibernate? One table for Person and another table for PhoneNumbers with a foreign key to PersonsId. That's what Hibernate will do unless you instruct it otherwise, as mentioned by partenon.
精彩评论