From: Gavin_King/Cirrus%<CI...@ci...> - 2002-03-05 01:22:30
|
> I was wondering how difficult it would be to support database native > keys in hibernate (i.e. indentity columns in sybase/mssql), or if theres > a reason why its not implemented atm in hibernate. If its really easy i > could take a look at implementing it ;) It would be great if you could get this going. It would certainly be a great feature to have. I think the main difficulty would be this: 1. Hibernate always delays executing SQL statements until a flush(), so you can't know what the generated key is until _after_ you execute the insert. 2. Hibernate uses the key in its internal data structures (and in other ways) so its expecting to know the key _before_ you execute the insert. Fixing this *should* be as simple as deciding to execute the insert immediately for some classes (and sticking with the current behaviour for others). Have a look at: (1) RelationalDatabaseSession.save(Object object, Serializable id) (2) ClassPersister.insert(insert(Serializable id, Object[] fields, SessionImplementor session) These two methods do the work of (1) * assigning an id, * calling PersistentLifecycle.create(), * copying the state of the object, * nullifying references to transient objects (in the copied state), * registering the object and its copied state in the sessions internal data structure, * scheduling the actual insertion for later (2) * actually executing the SQL insert The class ScheduledInsertion adds an indirection between save() and insert () that lets Hibernate execute the insertion "later". I suppose you would want to remove this indirection.... Good Luck! |