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