【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());