Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate- one-to-many association on a foreign key

 
Santosh Ramachandrula
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a one to many relationship why is "A unidirectional one-to-many association on a foreign key is a very unusual case" not recommended?

Please see the link below

http://www.hibernate.org/hib_docs/reference/en/html/assoc-unidirectional.html
 
Cristian Vrabie
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did followed this discussion about this, but there's not a clear answer. The best supposition argues that it's a much clearer DB design to use a join table but this surely doesn't explain why this is a strong recommendation.

It's a very interesting question. If anyone knows anything more, please do share. I'm very curious.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the Hibernate documenatation once again. In the many-to-one and one-to-many chapters carefully compare the mapping scheme with the create statements.

Do you see? Intuitively (and in real world) an address belongs to a person and not the reversed way. The same story goes on for many other cases. If you want to link the address with one or more persons using a key anyway, then you better need a join table so that you still can get the address by person.
[ December 25, 2008: Message edited by: Bauke Scholtz ]
 
Santosh Ramachandrula
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cristain and Bauke, Thanks for your response.

To me it seems to be a DB design issue rather than mapping. I think that the explanation of join table is apt for 2NF.

It also happens to that Hibernate has a way of converting many-to-many mapping TO one-to-many with unique="true" attribute on join table. http://www.hibernate.org/hib_docs/reference/en/html/assoc-unidirectional-join.html 7.3.1
[ December 29, 2008: Message edited by: Santosh Ramachandrula ]
 
Sunil Kumar Roy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if we have tables called ORDER and ORDER_FEATURES, then ONE-TO-MANY with a Foreign Key is a valid scenario. Here, the Foreign key column called ORDER_NUMBER will be in ORDER_FEATURES, referring to ORDER_NUMBER in ORDER table.
It would be Uni-Directional because the Order features do not have any menaing by themselves; they make sense only when they are embeded as part of the order object.
We do have some situations of this type.
So, I am not sure why this is referred as a very unusual case.
Kindly explain if I am wrong.
 
Sunil Kumar Roy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I am facing a Mapping issue with one-to-many association on a foreign key. Requesting Help.

In Table ACTIVITY, BUSNS_ACTVY_ID is the PK.

In Table ACTIVITY_SERVICE_ATTRIBUTE, SRVC_ATTRIB_BUSNS_ACTVY_ID is the FK, referring to BUSNS_ACTVY_ID in ACTIVITY.

[ERROR]
Repeated column in mapping for entity: com.custom.businessclasses.ActivityServiceAttribute column: SRVC_ATTRIB_BUSNS_ACTVY_ID (should be mapped with insert="false" update="false")
[/ERROR]




====================================================================================

So, I modified the followng line to include insert="false" update="false" .


<property name="businessActivityHistoryAssocId" column="SRVC_ATTRIB_BUSNS_ACTVY_ID" type="string" length="47" not-null="true" lazy="false"/>

to

<property name="businessActivityHistoryAssocId" column="SRVC_ATTRIB_BUSNS_ACTVY_ID" type="string" length="47" not-null="true" lazy="false" insert="false" update="false"/>


Then I got the following Error.


[ERROR]
Association references unmapped class: com.custom.businessclasses.ActivityServiceAttribute

[/ERROR]


Requesting Help.

Thanks,
Sunil
 
Rachit Chawla
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m too stuck on the issue particulary stated by Sunil Kumar Roy. If I add insertable and updatable false in my child entity, I wont be able to save it separately, which is also very much valid in my case, given I alredy have order_id with me.

If somebody already has a solution for the problem please help or guide to proper forum.

-Rachit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic