2

I'm using hibernate 5.0.12 with springboot 1.5.14.

My entity has this id

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "UserID", nullable = true)
public int getUserId() {
    return this.userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

In my database are presents record with id = 0.

If I read, edit and save these record, hibernate duplicates these record with new id generated.

How I configure hibernate to allow id = 0 value?

In eclispelink this configuration is "eclipselink.id-validation" = "NULL"

Derrick
  • 3,669
  • 5
  • 35
  • 50
Matt.B
  • 33
  • 3

3 Answers3

1

The solution is to declare the primary key type as Integer instead of int.

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "UserID", nullable = true)
public Integer getUserId() {
    return this.userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}
Matt.B
  • 33
  • 3
0

You should edit the database not the hibernate configurations although it's a bad practice.

I want id to start from 0,but starts from 1

Kroxitrock
  • 35
  • 11
  • It is not my request. the db already accepts 0 values. The records are already present with this value. – Matt.B Jul 09 '18 at 13:28
0

This question should rather be database-related because it's the database that usually "corrects" your entry even if you explicitly set zero value for your Long/Int primary key (due to the way the default settings are set in most databases).

I.e. in case of MySQL, you can explicitly set ID to zero (0) but the database will still ignore it unless you i.e. add/set the NO_AUTO_VALUE_ON_ZERO for your sql_mode. If that's the case for MySQL/MariaDB, you can change this directly in my.cnf file and if you don't want to reset the database you need to set it via set global sql_mode='NO_AUTO_VALUE_ON_ZERO'

I presumed this answer to be relevant because you utilize @GeneratedValue(strategy=GenerationType.IDENTITY)

SadmirD
  • 642
  • 3
  • 8
  • My database is SAP SQL Anywhere 17. It allow zero value for autoincrement fields by default. In my database, only null value is computed by autoincrement. – Matt.B Nov 08 '18 at 11:13