tk-mybatis连接pgsql插入数据主键id自增报错

Tk-Mybatis在PostgreSQL插入数据时主键ID自增问题解决
在使用Tk-Mybatis连接PostgreSQL数据库尝试插入数据时遇到主键ID违反非空约束的问题。错误信息显示null值违反了主键id的非空约束。解决方案是:在POJO类中,对于主键ID只需使用@Id注解,不需要额外的主键策略注解;在插入数据时,推荐使用insertSelective方法,它会忽略未赋值的ID,允许数据库自增序列生效。违反这些规则会导致插入失败。

学习的时候花了很久才解决这个问题,记录之。

(一)报错信息

1.错误信息情况1:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY,generator = "JDBC")
    private Long id;
    @Column
    private String account;
    @Column
    private String password;

### Error updating database.  Cause: org.postgresql.util.PSQLException: 错误: null value in column "id" of relation "user1" violates not-null constraint
  详细:失败, 行包含(null, Lily                                                            ..., 678976                                                          ...).
### The error may involve com.lzy.mapper.UserMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO user1  ( id,account,password ) VALUES( ?,?,? )
### Cause: org.postgresql.util.PSQLException: 错误: null value in column "id" of relation "user1" violates not-null constraint
  详细:失败, 行包含(null, Lily                                                            ..., 678976                                                          ...).
; 错误: null value in column "id" of relation "user1" violates not-null constraint
  详细:失败, 行包含(null, Lily                                                            ..., 678976                                                          ...).; nested exception is org.postgresql.util.PSQLException: 错误: null value in column "id" of relation "user1" violates not-null constraint
  详细:失败, 行包含(null, Lily                                                            ..., 678976                                                          ...).] with root cause

2.错误信息情况2

 @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "seq1")  //主键自增策略
    @SequenceGenerator(name="seq1",sequenceName ="user_id_seq",allocationSize = 1)
    private Long id;
    @Column
    private String account;
    @Column
    private String password;


Caused by: tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: id - 该字段@GeneratedValue配置只允许以下几种形式:
1.useGeneratedKeys的@GeneratedValue(generator=\"JDBC\")  
2.类似mysql数据库的@GeneratedValue(strategy=GenerationType.IDENTITY[,generator="Mysql"])

(二)问题解决

1.问题解决

        因为在pgsql的表中采用的是创建序列的方法来实现主键id自增,且主键id设置为非空,通过多次试验发现在用tk-mybatis时不能在pojo类的id属性上加任何的主键策略注解,只需要注解@Id即可,这不同于mysql.

     且在调用增加实体类方法时,最好调用userMapper.insertSelective(user)方法,不要用userMapper.insert(user)方法。

2.总结

 当pgsql中的主键id采用创建序列的方法自增时:

(1)在tk-mybatis中的pojo类中id属性的注解只用加@Id,其他任何主键策略注解都不要加

(2)在调用增加实体类方法时,最好调用userMapper.insertSelective(user)方法,会忽略掉未赋值的id值,通过数据库自增序列自增

如果上述任何一项不遵守,都会报错---错误: null value in column "id" of relation "user1" violates not-null constraint,因为表中id字段设置为非空主键, 而insert方法会给所有字段添加值,没有赋值的添加null

(三)具体原因

具体原因我也还在学习中,如果有大佬清楚的话,请多多指教与补充,谢谢~

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值