【BUG 记录】ested exception is org.postgresql.util.PSQLException: ERROR: operator does not exist: intege

【BUG 记录】ested exception is org.postgresql.util.PSQLException: ERROR: operator does not exist: intege

1. 问题

在实际开发过程想使用枚举字段,使用了 @EnumValue 方式的枚举,结果报错,其中 gender 是我的枚举字段

数据库中我是用int 存储,Java 用 GenderEnum 接收

我的枚举类为

@Getter
public enum GenderEnum {

    WOMAN(0, "女人"),

    MAN(1, "男人");

    @EnumValue
    private final Integer code;
    @JsonValue
    private final String desc;

    GenderEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

}

报错信息:

### Error querying database.  Cause: org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = character varying
  建议:No operator matches the given name and argument types. You might need to add explicit type casts.
  位置:90
### The error may exist in com/haoma/webpgsqldemo/mapper/TestUserMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT  id,name,age,gender,address,phone  FROM test_user     WHERE (name = ? AND gender = ?)
### Cause: org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = character varying
  建议:No operator matches the given name and argument types. You might need to add explicit type casts.
  位置:90
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = character varying
  建议:No operator matches the given name and argument types. You might need to add explicit type casts.
  位置:90] with root cause

2. 问题分析

打印日志

2024-04-02 17:40:30.248 DEBUG 28424 --- [nio-8080-exec-1] c.h.w.mapper.TestUserMapper.selectOne    : ==>  Preparing: SELECT id,name,age,gender,address,phone FROM test_user WHERE (name = ? AND gender = ?)
2024-04-02 17:40:30.273 DEBUG 28424 --- [nio-8080-exec-1] c.h.w.mapper.TestUserMapper.selectOne    : ==> Parameters: 小李(String), MAN(String)

可以从日志中看到底层调用的是mybatis selectOne 方法,并且第二个参数的参数类型为String ,基本上就可以锁定问题,枚举转换有问题,正常请开给你,这块应该传递的参数为 1 而不是MAN

3. 解决方案

1. 升级 mybatis-plus 版本

最后通过查阅资料,发现切换 mybatis-plus 版本就能解决
我当时使用的版本为 3.4.1 ,切换成最新版3.5.3之后就没问题了

2. 使用枚举的 get() 方法

如果不想切换版本,那么可以在判断的时候添加.getCode()方法,相当于强制类型转换了

queryWrapper.eq(TestUserEntity::getGender, entity.getGender().getCode());
  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JAVA开发区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值