I'm a Hibernate noob, thrust into modifying a Hibernate app. I've been reading up on things, but am still very lousy at hibernate.
Anyway, I have a table that had been given an artificial key. It is a audit table that is never updated or deleted from the application, only inserted to. A database trigger that relied on the key was found to be unnecessary and poor performing so it is being removed. Along the way, the analysis team wants to remove the artificial sequential key, and instead will go with a userId-timestamp combination composite key. Our legacy DB has alot of composite keys, so this is a perfect opportunity to learn how Hibernate handles composite keys, which other developers (also new to hibernate) have been telling me it doesn't handle well...or at all. I can't believe that a DB solution as widely used as Hibernate can't handle a situation as common as composite keys, even if it is not the "preferred" way, so I am eager to find out how Hibernate handles this.
But I am running into problems. The id portion of my hbm.xml looks like
Code:
<composite-id name="id" class="my.package.UmcUsrTransTId">
<key-property name="usrId" type="string">
<column name="USR_ID" length="60" not-null="true" />
</key-property>
<key-property name="updtTmstmp" type="timestamp" column="UPDT_TMSTMP" length="26" generated="always" />
</composite-id>
The result is
Code:
Caused by:
org.hibernate.MappingException: Invalid ORM mapping file.
Error parsing XML (line11 : column 113): Attribute "generated" must be declared for element type "key-property".
at org.hibernate.cfg.AnnotationConfiguration.addInputStream(AnnotationConfiguration.java:920)
at org.hibernate.cfg.AnnotationConfiguration.addInputStream(AnnotationConfiguration.java:107)
I presume this is telling me that the generated attribute is in the wrong place. The generated flag worked fine when timestamp was JUST a property, not a key-property. If I remove it, the error changes to an exception on insert because the timestamp is updated by the database. So I seem to be in a damned if you do situation where I can't have generated on the key-property, but I need it to avoid downstream exceptions.
I tried changing this to have a column tag under it with generated on it, but that didn't work either. I adapted the beans to match this structure. We actually have a composite-id table elsewhere in the app which works, but it doesn't have a generated timestamp in it.