当前使用版本

3.5.0

该问题是如何引起的?

当表的主键为自增长,由于sql server 应该有别于 mysql 插入机制,其 @TableId(value = "id",type = IdType.AUTO) 设置无效,sql server 抛出 Cannot insert explicit value for identity column in table 'students' when IDENTITY_INSERT is set to OFF. 的错误,打印原生 sql 其含有 id 字段,但 sql server 默认是不允许在插入语句中出现自增长字段。

重现步骤(相关测试代码如下)

entity 类

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("students")
public class Students {
    @TableId(value = "id",type = IdType.AUTO)
    private int Id;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    @TableField(value = "name")
    private String Name;

    public int getAge() {
        return Age;
    }

    public void setAge(int age) {
        Age = age;
    }

    @TableField(value = "age")
    private int Age;
}

测试代码

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    private StudentsMapper studentsMapper;

    @Test
    public void testInsert() {
        System.out.println(("----- insert method test ------"));
        Students students=new Students();
        students.setName("xiaoming");
        students.setAge(17);
        studentsMapper.insert(students);
    }
}

sql server client drivergroup: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '10.2.0.jre8'

报错信息

### Error updating database.  Cause: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'students' when IDENTITY_INSERT is set to OFF.
### The error may exist in com/example/demo/mapper/StudentsMapper.java (best guess)
### The error may involve com.example.demo.mapper.StudentsMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO students  ( id, name, age )  VALUES  ( ?, ?, ? )
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'students' when IDENTITY_INSERT is set to OFF.
; Cannot insert explicit value for identity column in table 'students' when IDENTITY_INSERT is set to OFF.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'students' when IDENTITY_INSERT is set to OFF.
org.springframework.dao.DataIntegrityViolationException: 
### Error updating database.  Cause: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'students' when IDENTITY_INSERT is set to OFF.
### The error may exist in com/example/demo/mapper/StudentsMapper.java (best guess)
### The error may involve com.example.demo.mapper.StudentsMapper.insert-Inline
### The error occurred while setting parameters

Comment From: miemieYaho

private Integer Id;

Comment From: qiandu23

可以了,后面可以兼容int 和 integer 吗?

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("students")
public class Students {
    @TableId(value = "id",type = IdType.AUTO)
    private Integer Id;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    @TableField(value = "name")
    private String Name;

    public Integer getAge() {
        return Age;
    }

    public void setAge(int age) {
        Age = age;
    }

    @TableField(value = "age")
    private Integer Age;
}