File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate - Legacy Database Question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "Hibernate - Legacy Database Question" Watch "Hibernate - Legacy Database Question" New topic
Author

Hibernate - Legacy Database Question

Jim Cross
Greenhorn

Joined: Nov 20, 2003
Posts: 17
I have a table in my database which is versioned, so each record consists of ID, VERSION, TRADE_DATE, VALUE_DATE, etc where the primary key is a composite of ID and VERSION.
So a trade with ID 12345 starts with VERSION 1, and this version number gets incremented on each update.
Is there a recommended pattern for handling this in Hibernate? My current understanding is that I need to use the composite-id tag, and create a TradeKey inner class in my Trade bean class, with the TradeKey class holding the ID and VERSION. I then have to handle the generation of trade ids and version myself. Is this the best way to do it?
If so, is there any way to create my own SQL prepared statements using Hibernate? I know you can create normal SQL queries, but I'd ideally like to use a prepared statement which parameterises the object type (my key generator table in the database as it is currently holds keys for various object types - trade, cashflow etc). If possibly I'd like to query and update this table using a prepared statement. If this isn't possible using Hibernate, I'll have to do this part of it using standard JDBC, which I'd rather avoid...

Also, if I was looking to refactor this trade table to make it more suitable for Hibernate in the future, what would be the best way to structure the primary key, given that each trade must maintain the "trade id" and incremented version number throughout its life?

Any help much appreciated.

Thanks,

Jim
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
the primary key is a composite of ID and VERSION


If you update entity with ID 12345 from version 1 to version 2, do you keep version 1 in the table and insert version 2?

a TradeKey inner class


TradeKey can be an inner class, but it doesn't have to be.

is there any way to create my own SQL prepared statements using Hibernate?


Yes, using a Hibernate named query is one way.
[ July 16, 2006: Message edited by: Scott Johnson ]
Jim Cross
Greenhorn

Joined: Nov 20, 2003
Posts: 17
Thanks Scott.

If you update entity with ID 12345 from version 1 to version 2, do you keep version 1 in the table and insert version 2?


Yes, so it's update as insert, and we maintain the full history of each trade in the table.
Jim Cross
Greenhorn

Joined: Nov 20, 2003
Posts: 17
OK, that solved that problem

Another question related to this.
Trades have a one-to-many relationships with cashflows. So the cashflow table contains the columns TRADE_ID, TRADE_VERSION, which are a foreign key into the TRADE table.

Using the following in my Hibernate mapping file, when I load a trade it now loads a set of cashflows. However, while it populates most of the properties correctly, it doesn't populate the tradeId and tradeVersion properties of the cashflows...



I guess this is because the column element only specifies the database column, and not the Java bean property to map that column on to. Any ideas how I can specify this?
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
Any ideas how I can specify this?


I've not tried this before, so this is a wild guess... But can you add the column <property> tags even though they are already in the <key>:

Jim Cross
Greenhorn

Joined: Nov 20, 2003
Posts: 17
I tried that yesterday, but it didn't allow me to specify those columns in both the key and the composite element.
However, I finally got it working by doing the right way (I think!):

 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Hibernate - Legacy Database Question
 
Similar Threads
How to insert a new child record only. One to Many Relation.
How to Determine Whether to Insert or Update
Hibernate - Force foreign key lookup on insert
Hibernate Mapping- composite id (pk+ foreign key)
what if no ID column in the table for Hibernate?