multiple database support for same JPA classs

让人想犯罪 __ 提交于 2019-12-17 21:16:19

问题


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.


回答1:


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).




回答2:


@Id
    @SequenceGenerator(name="admin_seq", sequenceName="unique_id")
    @GeneratedValue(strategy=GenerationType.AUTO, generator="admin_seq")
    private Long id

worked for me , thanks for all your answers



来源:https://stackoverflow.com/questions/11611046/multiple-database-support-for-same-jpa-classs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!