JAVA往SQLServer插入数据

1.当 IDENTITY_INSERT 设置为 OFF 时,不能为表 '{表名}' 中的标识

原来的实体类如下,因为“int”默认值为0,造成ruqId=0而报错

private int ruqId;

@Id
@Column(name = "RUQ_ID", unique = true, nullable = false)
public int getRuqId() {
    return ruqId;
}

public void setRuqId(int ruqId) {
    this.ruqId = ruqId;
}

修改为“Integer”:

private Integer ruqId;

@Id
@Column(name = "RUQ_ID", unique = true, nullable = false)
public Integer getRuqId() {
    return ruqId;
}

public void setRuqId(Integer ruqId) {
    this.ruqId = ruqId;
}

2. ids for this class must be manually assigned before calling save()

加上“@GeneratedValue(strategy = GenerationType.AUTO)”部分,作为主键的生成策略。即可解决~

private Integer ruqId;

@Id
@Column(name = "RUQ_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getRuqId() {
    return ruqId;
}

public void setRuqId(Integer ruqId) {
    this.ruqId = ruqId;
}

3. 在SQLserver上让某个字段自增时

20191012110145.png

因为表里面已经有了“GROUP_ID”,将它删掉就OK了

你可能感兴趣的:(JAVA往SQLServer插入数据)