JPA 报错解决方案 com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'test_db' when IDENTITY_INSERT is set to OFF.

这种错误插入数据时就是hibernate的自增长字段生成规则应该用native

 

在字段前加入注解 

@GeneratedValue(generator="generator")

@GenericGenerator(name="generator", strategy = "native")

 

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.GenericGenerator;;

@Entity(name="test_db")
@Embeddable

public class BasicInfoEntity {
    @Column(name = "id")
    @Id
    @GeneratedValue(generator="generator")
    @GenericGenerator(name="generator", strategy = "native")
    private int id;
    @Column(name = "name")
    private String name;
    @Column(name = "sex")
    private String sex;
    @Column(name = "height")
    private String height;
    @Column(name = "weight")
    private String weight;
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getHeight() {
        return height;
    }
    public void setHeight(String height) {
        this.height = height;
    }
    public String getWeight() {
        return weight;
    }
    public void setWeight(String weight) {
        this.weight = weight;
    }

}

 

 

 

 

可以参考此链接:

https://www.cnblogs.com/tobeprogramer/p/hibernate_pojo_annotation.html

 

 

posted @ 2019-06-26 14:22  bolddream  阅读(1329)  评论(0编辑  收藏  举报