• 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

     

  • 相关阅读:
    2018/1/1 XML和DOM、SAX解析
    2018/1/1 Html+CSS+JavaScript
    2017/12/30 GUI和动态代理
    开发运维实施一系列问题归类
    如何理解并发,并行,串行
    java内存溢出与内存泄漏
    IDEA 导入的聚合工程父工程报错Cannot resolve symbol 'modelVersion'
    jvm垃圾回收算法
    String创建对象的难题一
    String详解
  • 原文地址:https://www.cnblogs.com/bolddream/p/11089627.html
Copyright © 2020-2023  润新知