|
From: Colin S. <col...@ex...> - 2003-10-20 20:02:24
|
jürgen höller [werk3AT] wrote: >Thomas, Trevor, > >>oes Hibernate use a different ID strategy for different databases? I think >> >> >what is implemented in the Petclinic is a strategy that would work the same >across different databses as long as there is a MaxValueIncrementer implementation. > >I intend to configure Hibernate to use "identity" for all databases. Setting the correct Hibernate dialect will lead to "auto_increment" on MySQL and "identity" on HSQL then. In terms of the data model, this simply means dropping the sequence tables and defining the id columns as "auto_increment" 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 columns. The disadvantage is that the mechanism is normally not native to the database and thus somewhat tied to the application. > >>f we change the Petclinic JDBC implementation to use identity columns, then we >> >> >need to figure out how to run the insert and subsequent query to retrieve 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 without 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 already uses MySQLJdbcClinic and HSQLJdbcClinic subclasses; it should be easy to encapsulate the id-fetching query there. > >Of course, the range of possible Petclinic JDBC implementations will then be bound to identity-supporting databases. According to the Hibernate docs, those are "DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL". Oracle 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 on both MySQL and HSQL that way! > > I guess you are trying to make the exact same thing run under both Hibernate and JDBC, but if you are willing to say the Oracle DB only works with the Hibernate version (and handle the DDL separately too), then you can easily do that by just using 'native' as the Hibernate type... |