|
From: Ken K. <kk...@kk...> - 2003-10-20 22:03:18
|
It might be most useful to implement one of the 2 databases as is using=20 the MaxValueIncrementer while implementing the other using Hibernate to=20 preserve both types of examples. Ken j=FCrgen h=F6ller [werk3AT] wrote: >Thomas, Trevor, > > =20 > >>Does Hibernate use a different ID strategy for different databases? I = think >> =20 >> >what is implemented in the Petclinic is a strategy that would work the s= ame >across different databses as long as there is a MaxValueIncrementer impl= ementation. > >I intend to configure Hibernate to use "identity" for all databases. Set= ting the correct Hibernate dialect will lead to "auto_increment" on MySQL= and "identity" on HSQL then. In terms of the data model, this simply mea= ns dropping the sequence tables and defining the id columns as "auto_incr= ement" respectively "identity" in the DDL scripts. > >Sure, MaxValueIncrementer adopts a very generic approach that will work = on any database, even if it doesn't support such a thing like identity co= lumns. The disadvantage is that the mechanism is normally not native to t= he database and thus somewhat tied to the application. > > =20 > >>If we change the Petclinic JDBC implementation to use identity columns,= then we >> =20 >> >need to figure out how to run the insert and subsequent query to retriev= e the id >using the same connection whether we are in a transaction or not. > >This is very similar with MySQL and HSQL: You simply run the insert with= out specifying a value for the id field, and query "select last_insert_id= ()" respectively "call identity()" afterwards (I've looked up the latter = in Hibernate's MySQLDialect and HSQLDialect implementations). Petclinic a= lready uses MySQLJdbcClinic and HSQLJdbcClinic subclasses; it should be e= asy to encapsulate the id-fetching query there. > >Of course, the range of possible Petclinic JDBC implementations will the= n be bound to identity-supporting databases. According to the Hibernate d= ocs, those are "DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL". Ora= cle is notably absent; I think we can live with Petclinic not running on = Oracle, if we can allow for a dynamic switch between JDBC and Hibernate o= n both MySQL and HSQL that way! > > =20 > >>>auto-increment just allows to read in the actual id afterwards >>> =20 >>> >>This is not reliable for concurrent inserts (which is why I generally u= se sequences. For the sample this should be sufficient though. >> =20 >> > >According to what I found out about MySQL, "last_insert_id" returns the = last used id on the same connection and is thus transactionally safe. Don= 't know about HSQL, but I frankly don't mind for sample purposes. > >Juergen > > >------------------------------------------------------- >This SF.net email sponsored by: Enterprise Linux Forum Conference & Expo >The Event For Linux Datacenter Solutions & Strategies in The Enterprise=20 >Linux in the Boardroom; in the Front Office; & in the Server Room=20 >http://www.enterpriselinuxforum.com >_______________________________________________ >Springframework-developer mailing list >Spr...@li... >https://lists.sourceforge.net/lists/listinfo/springframework-developer > > > =20 > |