简体   繁体   中英

How to insert value into primary key in SQL server by using java + hibernate?

I get this error message when trying to run this java code by using hibernate:

create table EMPLOYEE (
    ID integer not null,
    JOINING_DATE date not null,
    NAME varchar(50) not null,
    SALARY decimal(10,2) not null,
    SSN varchar(255) not null,
    primary key (ID)
)

My Java code is mention below and using hibernate, I need to insert the value in DB.

@Entity
@Table(name="EMPLOYEE")
public class Employee {

    @Id
    @Column(name = "ID", nullable = false, insertable = false, updatable = false)
    @TableGenerator(name="ID" ,table="EMPLOYEE", allocationSize=1, initialValue=1)
    private int id;

    @Size(min=3, max=50)
    @Column(name = "NAME", nullable = false)
    private String name;

But while inserting the value I am getting below error:

threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK__EMPLOYEE__3214EC27B6653156'. Cannot insert duplicate key in object 'dbo.EMPLOYEE'. The duplicate key value is (0).
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:390)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
    at  

When creating the table, you should indicate that the ID column is the primary key and that the database should be in control of assigning the value:

 id integer NOT NULL IDENTITY PRIMARY KEY,

I'm not familiar with the @TableGenerator annotation, a more common approach is to annotate the id field with:

@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)

IDENTITY Indicates that the persistence provider must assign primary keys for the entity using a database identity column. More information...

thus letting Hibernate know that the value of this field is controlled by the database.

I hope this helps.

The documentation of the @TableGenerator says:

Defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation.

This means that the id field should additionally have @GeneratedValue annotation eg @TableGenerator(name=" myTableGen " ,table="xxx", allocationSize=1, initialValue=1) @GeneratedValue(strategy=TABLE, generator=" myTableGen ")

So the attributes of the @TableGenerator could be explained as follows: name: A unique generator name that can be referenced by one or more classes to be the generator for id values. table: the table that stores the primary key values pkColumnName: the column inside the table that stores the primary key values ...

Reference is https://docs.oracle.com/javaee/7/api/javax/persistence/TableGenerator.html

However @TableGenerator means that a separate table is used to hold the generated ids. If you don't want this overhead and simply want to generate values in the ID column, you can use the other generator types eg Sequence or Identity

尝试使用

private Integer id;

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