aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate- one-to-many association on a foreign key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate- one-to-many association on a foreign key" Watch "Hibernate- one-to-many association on a foreign key" New topic
Author

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

Santosh Ramachandrula
Ranch Hand

Joined: Apr 04, 2004
Posts: 252
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


Thanks,
Santosh
Cristian Vrabie
Ranch Hand

Joined: Jul 09, 2008
Posts: 71
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Apr 04, 2004
Posts: 252
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

Joined: Dec 31, 2008
Posts: 2
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

Joined: Dec 31, 2008
Posts: 2
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

Joined: Mar 23, 2009
Posts: 1
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate- one-to-many association on a foreign key