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