Java 在数据迁移时,获取错误:运算符不存在:boolean=integer提示:没有与给定名称和参数类型匹配的运算符

Java 在数据迁移时,获取错误:运算符不存在:boolean=integer提示:没有与给定名称和参数类型匹配的运算符,java,mysql,postgresql,spring-boot,jpa,Java,Mysql,Postgresql,Spring Boot,Jpa,将数据库mysql v5迁移到postgres v12后,Java Spring应用程序显示以下错误: 错误:运算符不存在:布尔值=整数 提示:没有与给定名称和参数类型匹配的运算符。您可能需要添加显式类型转换。这是因为您在PostgreSQL表中创建了一个布尔类型的列。在MySQL中,布尔值表示为整数值(通常为位),以节省空间,PostgreSQL中没有隐式转换: psql (12.4) Type "help" for help. postgres=# select tru

将数据库mysql v5迁移到postgres v12后,Java Spring应用程序显示以下错误: 错误:运算符不存在:布尔值=整数
提示:没有与给定名称和参数类型匹配的运算符。您可能需要添加显式类型转换。

这是因为您在PostgreSQL表中创建了一个布尔类型的列。在MySQL中,布尔值表示为整数值(通常为位),以节省空间,PostgreSQL中没有隐式转换:

psql (12.4)
Type "help" for help.

postgres=# select true = 1;
ERROR:  operator does not exist: boolean = integer
LINE 1: select true = 1;
                    ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
postgres=# 
您可以编写一个函数,将位转换为布尔值,或者反过来,然后创建隐式转换:

-- you'll need to write your own `bit_to_boolean()` function
CREATE CAST (BIT AS BOOLEAN)
  WITH FUNCTION bit_to_boolean(BIT)
  AS IMPLICIT;
那可能是太多的工作了。您最好在Java中将int转换为String,然后以这种方式进行比较

postgres=# select true = 1;
ERROR:  operator does not exist: boolean = integer
LINE 1: select true = 1;
                    ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
postgres=# select true = '1';
 ?column? 
----------
 t
(1 row)

postgres=# select true = 't';
 ?column? 
----------
 t
(1 row)

postgres=# select true = 'f';
 ?column? 
----------
 f
(1 row)

postgres=# select true = '0';
 ?column? 
----------
 f
(1 row)
另一种可能解决您看到的问题的方法是编辑Java代码以比较真/假关键字,而不是整数:

-- do this
SELECT * FROM table WHERE bool_col = <true/false>;

-- instead of this
SELECT * FROM table WHERE bool_col = <val>;

这是因为您在PostgreSQL表中创建了一个布尔类型的列。在MySQL中,布尔值表示为整数值(通常为位),以节省空间,PostgreSQL中没有隐式转换:

psql (12.4)
Type "help" for help.

postgres=# select true = 1;
ERROR:  operator does not exist: boolean = integer
LINE 1: select true = 1;
                    ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
postgres=# 
您可以编写一个函数,将位转换为布尔值,或者反过来,然后创建隐式转换:

-- you'll need to write your own `bit_to_boolean()` function
CREATE CAST (BIT AS BOOLEAN)
  WITH FUNCTION bit_to_boolean(BIT)
  AS IMPLICIT;
那可能是太多的工作了。您最好在Java中将int转换为String,然后以这种方式进行比较

postgres=# select true = 1;
ERROR:  operator does not exist: boolean = integer
LINE 1: select true = 1;
                    ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
postgres=# select true = '1';
 ?column? 
----------
 t
(1 row)

postgres=# select true = 't';
 ?column? 
----------
 t
(1 row)

postgres=# select true = 'f';
 ?column? 
----------
 f
(1 row)

postgres=# select true = '0';
 ?column? 
----------
 f
(1 row)
另一种可能解决您看到的问题的方法是编辑Java代码以比较真/假关键字,而不是整数:

-- do this
SELECT * FROM table WHERE bool_col = <true/false>;

-- instead of this
SELECT * FROM table WHERE bool_col = <val>;

布尔类型检查因数据库而异,例如mysql和postgres。考虑下面的例子我所经历的。 基本实体类BaseEnity{}有一列为活动布尔类型,Order{}实体类扩展了该类。要选择所有活动订单,mysql查询为:

select * from Order where active = 1
但将数据库迁移到postgres时,它不起作用。在postgres中,它显示错误运算符不存在:boolean=integer。正如postgres所期望的那样:

select * from Order where active = true

因为postgres需要布尔值true/false,但在SQL查询中,该值被设置为整数类型1,我们在提示中遇到错误。

布尔类型检查因数据库而异,例如mysql和postgres。考虑下面的例子我所经历的。 基本实体类BaseEnity{}有一列为活动布尔类型,Order{}实体类扩展了该类。要选择所有活动订单,mysql查询为:

select * from Order where active = 1
但将数据库迁移到postgres时,它不起作用。在postgres中,它显示错误运算符不存在:boolean=integer。正如postgres所期望的那样:

select * from Order where active = true
因为,postgres需要布尔值true/false,但在SQL查询中,该值被设置为整数类型1,我们遇到了提示错误