Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Object Relational Mapping and the fly likes Cascading non foreign key to Child Table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Cascading non foreign key to Child Table " Watch "Cascading non foreign key to Child Table " New topic

Cascading non foreign key to Child Table

Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
I have a parent table, t_books_category and a corresponding child table T_Book with a 1-n mapping. My configuration files for the two tables are as under:

Mapping for T_BOOKS table


As you can see I have been able to map the foreign key attribute of cat_id. However there is one further requirement, there is an additional column cat_Date which is also present in the child table and both values will be the same. However this field is a non key field in both the tables. How do i perform the mapping for this column such that save and update on the master table is cascaded to the child table too.

Thank you and I would appreciate any help.

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Hibernate is not going to replicate denormalized data for you unfortunately. You'll need to keep these updated manually (or implement a trigger to do it).

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
Thank you Paul. I can achieve what I was trying out programmatically, I was just wondering if hibernate can do the same.
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 661

Hibernate has something called as dynamic-updates and dynamic-inserts.
If the child objects are loaded and if you are updating the category date in category, then when you change the category date in books hibernate automatically detects a change in value and fires a update query only to update the specified column.

Now I know that this feature directly works on the object, but I'm not sure if it works on the children associated with the object. You might want to try it.
This wouldn't work if you dates are generated automatically by the database.

If you are not laughing at yourself, then you just didn't get the joke.
I agree. Here's the link:
subject: Cascading non foreign key to Child Table
It's not a secret anymore!