Enable <generator> tag for non primary key columns that are autogenerated or is an identity
Description
Activity
Steve Ebersole March 28, 2007 at 8:15 PM
As I mentioned before if you do not want to fix your broken schema, thats fine; you can still make this work by marking the non-PK surogate value as a generated value in you mappings. This will work as is for IDENTITY columns; for sequences you'll need to use a trigger or column-level DEFAULT definition to ge the sequence value into the column. But I am not going to add support specifically for what you are asking in Hibernate.
http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-generated
Look at the tests in the testsuite testing this feature for examples: http://fisheye.jboss.com/browse/Hibernate/trunk/Hibernate3/test/org/hibernate/test/generated
Manoj Pallikkattil March 28, 2007 at 7:45 PM
Steve, I understand this is probably a bad database design. But it is going to be a very huge effort for us to fix the database design to make it work. We have more than 10 production clients with most of them having 10gig database sizes. If I would start off with a new database design I probably will not do this again. Only reason I am pursuing this is because I am using an existing database and I want to use hibernate. Let me ask you this, how big of an effort is it for hibernate to enable autogeneration for non primary key columns ? If you say it is going to be a huge one then I am ok with not pursuing it. But if this a matter of few hours or a day, it might be worth it since other people are experiencing similar issues. And with this feature enabled hibernate will only be able to support more datababase schemas. So it is not a bad thing. Well just my 2 cents. Thanks.
Also as per your comments above about using properties tag, I was able to make hibernate use my sequence column as primary key and my guid column as a unique key using the properties tag and use this key to join other tables. The issue with this one is that I cannot enable schema verification since the PK's are reversed in hibernate.
Steve Ebersole March 28, 2007 at 4:23 PM
Yes, a table with two distinct surogate keys is not justification in my mind 😉 I can just hear my DBA friends cringing 🙂)
Joseph Toth March 28, 2007 at 5:41 AM
I have a use case for using an IDENTITY column for something other than a PK.
I use GUIDs for PKs for all my tables. The business needs an incremental number for a piece of data, so I need an incremental column.
I'm not saying Hibernate should support this, I'm just throwing that out there. 🙂
Steve Ebersole March 14, 2007 at 11:41 PM
I'm sorry, but use of an IDENTITY column makes no sense as anything other than a PK. And anyway, if you really want to do this, you can via the generated properties values which are covered in the documentation.
Currently hibenate allow <generator> tag for primary keys, i.e inside the <id> tag. It would be good if we can use the generator tag for non primary key columns also. Our current datamodel has a UUID char 32 as the primary key and ID column which is a sequence for Oracle and an Identity for SQL Server is a unique key. I could get this to work with Oracle using Parameterized User Type but I have not been able to achieve this for SQL Server since the identity column is filled on the insert. If we could use the <generator> tag inside a <property> tag this would resolve this issue.