简体   繁体   中英

How can I force positive auto generated hibernate primary keys

I am using JPA 2.1 with Hibernate 4.3.8. on Oracle, MySQL, PostgreSQL and MS SQL. For creating numeric primary keys I have used the following code :

  @Id
  @Column(name = "ID")
  @GeneratedValue(strategy = GenerationType.AUTO, generator = "t_gen")
  @SequenceGenerator(name = "t_gen", sequenceName = "T_SEQ")
  private long id;

So far so good. But I need to generate only positive values of IDs under all conditions. How can I force Hibernate to generate only positive ID's on each database (MS SQL, MySQL, PostgreSQL, Oracle) ?

Hope this helps;

If allocationSize is not declared on the entity side, as default allocation size is 50 ALL sequences MUST declare INCREMENT BY 50 on the DB side.

Reference Link: http://skay-dev.blogspot.in/2013/09/hibernate-sequence-and-negative.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM