开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜