title: How to change Hibernate GenerationType identifier depending on the underlying database
body: <p>I have a project called <strong>main_project</strong> (uses spring-boot v2) that contains all configuration classes including <strong>JPA configuration</strong>. The <strong>main_project</strong> also has entity classes (Like User, Personnel).</p>
  
  <p>The JPA configuration for <strong>managed entity class</strong> in <strong>main_project</strong> is like below:</p>
  
  <pre><code>@Entity
  public abstract class MainEntity&lt;T extends Serializable&gt; {
      @Id
      @GeneratedValue(GenerationType=?)
      @Column(name = "id")
      private T id;
  }
  
  @Entity
  public class Personnel extends MainEntity&lt;Long&gt; {
      @Column(name = "firstName")
      private String firstName;
  
      // other proprties
  }
  </code></pre>
  
  <p>Other projects are using <strong>main_project</strong> as dependency for bootstrapping. Other projects that depends on <strong>main_project</strong> can use Personnel or User and ... entities and they can have different DBMS's like <strong>MySQL</strong> or <strong>Oracle</strong>.</p>
  
  <p>When i used <strong>main_project</strong> as a dependency in project <strong>A</strong> , entity class of <strong>A</strong> project <code>extends MainEntity&lt;?&gt;</code> and creates it's own entity classes and have its own database configuration file.</p>
  
  <p>my problem is on type of DBMS and <strong>GenerationType</strong> on <strong>id</strong> property in <strong>main_project</strong>.</p>
  
  <ul>
  <li><p>1) when <strong>A</strong> project uses <strong>Mysql</strong> database, <strong>MainEntity</strong> must have:</p>
  
  <p><code>@GeneratedValue(strategy = GenerationType.IDENTITY)</code></p></li>
  <li><p>2) when <strong>A</strong> project uses <strong>Oracle</strong> database, <strong>MainEntity</strong> must have:</p>
  
  <p><code>@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_generator")</code>
  <code>@SequenceGenerator(name="id_generator", sequenceName = "id_seq", allocationSize=1)</code></p></li>
  </ul>
  
  <p>How project A can detect its database type and switch between above approaches? </p>
  
  <p><em>I think that i need reflected java util that in runtime and add some annotations based on databases type! is it right?</em></p>
  
  <p>I also have read, <a href="https://stackoverflow.com/questions/19875993/what-strategy-is-actually-selected-for-generationtype-auto-for-the-major-datab">this_post_19875993</a> but it didn't helped.</p>
  
  <p><a href="https://stackoverflow.com/questions/30731627/hibernate-auto-key-generation-with-mysql-and-oracle">this_post_30731627</a> explained that we can select one of custom <strong>GenerationType</strong> but i want to select automatically without any changes to <strong>main_project</strong>, because <strong>A</strong> project can not change <strong>MainEntity</strong> class of <strong>main_project</strong> and just can used it.</p>