在spring / hibernate中切换数据库

时间:2017-10-19 07:39:28

标签: spring hibernate jpa spring-data

在我的项目中,我使用Spring和hibernate。我使用MySql并为Ids使用自动增量。但现在我需要支持多种数据库类型。 (单独安装)。说,MySql,Oracle(11g),Postgresql等

我目前的想法是使用uuid作为主键,因为我可以切换到任何数据库,而不必担心数据库层。但由于我使用Integer进行auto_increment,我必须修改我的代码库。

有没有办法保留整数ID?或者我应该继续使用uuid?

当前实施

@Id
@GeneratedValue
@Column(name = "id", nullable = false, updatable = false)
private Integer id;

或者,(或任何其他解决方案)

    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    @Column
    @Id
    private String id;

1 个答案:

答案 0 :(得分:0)

我找到了一种方法。

想法是添加注释配置并使用xml覆盖它。

XML metadata may be used as an alternative to these annotations, or to override or augment annotations

这是一个很好的教程,

https://vladmihalcea.com/how-to-replace-the-table-identifier-generator-with-either-sequence-or-identity-in-a-portable-way/

相关问题