多个数据库支持相同的JPA类 [英] multiple database support for same JPA classs

查看:126
本文介绍了多个数据库支持相同的JPA类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用MYSQL和Hibernate作为我们的项目。

JPA用于在DB中保存对象。 >我们有多个具有类似代码的类

  @Entity 
@Table(name =users)
类用户实现Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;



public Long getId(){
return id;
}

public void setId(Long id){
this.id = id;
}
}

现在我们也想支持oracle。我们应该怎么做?
strategy = GenerationType.AUTO不被oracle支持。

我们可以在每个POJO中定义我们不想做的序列吗? p>

请为我们提供一些意见,以便我们可以继续前进。 AUTO战略也应该在Oracle上运行。与MySQL的区别在于它将使用一个序列,而不是依赖于auto_increment ID。

如果需要,您甚至可以控制每个实体的序列名称:请参阅 oracle上的Hibernate序列,@GeneratedValue(strategy = GenerationType.AUTO)


We using MYSQL and Hibernate for our project.

JPA is used to persist object in DB.

We have Multiple class with similar code

@Entity
@Table(name = "users")
class Users implement Serializable {
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Long id;
    .
    .    
    .
   public Long getId() {
    return id;
   }

   public void setId(Long id) {
    this.id = id;
   }
}

Now we want to give support for oracle too. How should we do it ? strategy=GenerationType.AUTO is not supported by oracle.

One soln is we can define sequence in each POJO which we do not want to do?

please provide us some input so that we can move ahead.

解决方案

The AUTO strategy should work on Oracle as well. The difference with MySQL is that it will use a sequence instead of relying on an auto_increment ID.

You can even control the sequence name per entity if desired: see Hibernate sequence on oracle, @GeneratedValue(strategy = GenerationType.AUTO).

这篇关于多个数据库支持相同的JPA类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆