From: Christoph S. <ch...@sc...> - 2002-03-05 14:05:48
|
Hi Gavin! Thanks for the pointers. I implemented native key generation now for mssql server in a quite raw fashion, and I can send you the patches after some more tests. What I did: * Created A new key generator (NativeGenerator), and put it into the idgenerators map. This generator returns null as id. * Added a public boolean isNative function to the IDGenerator interface. All old generators return false here, the NativeGenerator returns true. * removed the id column from the insert if the idgen is native. * RelationalDatabaseSession.save(Object object, Serializable id) now calls the insert instead of creating a ScheduledInsert, but only if id == null * ClassPersister.insert(insert(Serializable id, Object[] fields,SessionImplementor session) now returns the generated id as Serializable. Whats still left to do is get the identity select from the DatabaseDialect, and modify the schema generation to generate identity columns. regards chris Gavin_King/Cirrus%CI...@ci... wrote: >>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! > > > |