title: Hibernate 5 ID AUTO Generation Type for Oracle as Sequence and MySQL as Identity
body: <p>We have a Java program using Hibernate 5 that can be installed using different DB Engines: Oracle, PostgreSQL or SQL Server. Now we are introducing MySql.</p>
  
  <p>The entities have a <em>Generated Value AUTO</em> identifier.</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  protected Long id;
  </code></pre>
  
  <p>This worked fine until now. Oracle and PostgreSQL installations are using HIBERNATE_SEQUENCE sequence to generate the id values and SQL Server is using a table generated id (IDENTITY).</p>
  
  <p>With MySQL we would like to use also the table generated id, but Hibernate 5 GenerationType.AUTO on MySQL tries the TABLE generator instead of the IDENTITY generator.</p>
  
  <p>I found <a href="https://vladmihalcea.com/why-should-not-use-the-auto-jpa-generationtype-with-mysql-and-hibernate/" rel="nofollow noreferrer">this solution</a> by Vlad Mihalcea where using a native generator makes Hibernate to select IDENTITY for MySQL:</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
  @GenericGenerator(name = "native", strategy = "native")
  protected Long id;
  </code></pre>
  
  <p>That's nice for MySQL, but then the application is no longer working in Oracle (ORA-02289: sequence does not exist). <a href="https://thoughts-on-java.org/hibernate-tip-hibernate-native-id-generator/" rel="nofollow noreferrer">I think</a> Hibernate is then searching for a sequence called native in the Oracle DB.</p>
  
  <p>Is there a solution to keep the ID generation the same for all the other DB engines (Oracle, PostgreSQL and SQL Server) and use the IDENTITY for MySQL?</p>
  
  <p>If not the only solution I can see is to implement the TABLE ID generation in MySQL, but it seems <a href="https://vladmihalcea.com/why-you-should-never-use-the-table-identifier-generator-with-jpa-and-hibernate/" rel="nofollow noreferrer">not to have a good performance</a>.</p>