Search code examples
javahibernatehibernate-mapping

Using ID generator "Sequence" or "Table HiLow" based on database


We are using Hibernate 3.2 and cannot upgrade due to dependency on other libraries being used in legacy applications. Currently, "native" id generator is being used in hibernate mapping file for table. In turn, hibernate uses "sequence" for Oracle and "Auto_Increment column" for MySQL. As application generates lot of Ids so in case of Oracle, to fetch next sequence lot of queries flows between database and application impacting performance. We would like to use HiLow for Oracle and would like to continue to use "Auto_Increment column" for MySQL.

Question is, How shall we define mapping file so as Hibernate should use HiLow if underlying database is Oracle and "Auto_Increment column" if underlying database is MySQL? If we cann't achieve this via mapping file, which class shall we override?


Solution

  • We have extended Oracle10gDialect and overridden method "supportsSequences()" to return false. This way mapping file continued to use native id generator and as configured Oracle dialect returns false for supporting sequences, Hibernate uses "TableHiLoGenerator".