I am using Spring Data R2DBC with MySQL, and I'm facing an issue with type conversion when mapping a database column of type INT to a Boolean field in my entity.
Entity Definition:
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
@Table("users")
public class UserEntity {
@Id
private Long id;
@Column("sms_list")
private Boolean smsPermited;
public Boolean getSmsList() {
return this.smsPermited;
}
public void setSmsList(Boolean smsPermited) {
this.smsPermited = smsPermited;
}
public Boolean getSmsPermited() {
return smsPermited;
}
public void setSmsPermited(Boolean smsPermited) {
this.smsPermited = smsPermited;
}
}
Database Schema:
CREATE TABLE users (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
sms_list INT(11) NOT NULL DEFAULT 0
);
When running the application, I'm encountering the following error:
No converter found capable of converting from type [java.lang.Integer] to type [java.lang.Boolean]
It seems like Spring Data R2DBC does not automatically convert the INT database column to a Boolean in my entity.
What I've Tried:
1.Using @Column on Getter:
2.Manually Handling Conversion in Getter/Setter::
3.Global Converters:::
Expected Behavior
I want to:
Questions:
Spring Boot and Library Versions:
If you're using io.asyncer.r2dbc-mysql version >= 1.3.0
, you can resolve this by changing the column type in your database schema from INT(11)
to BIT(1)
. This ensures proper mapping between the database column and the Boolean field in your entity.
Updated Schema
CREATE TABLE users (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
sms_list BIT(1) NOT NULL DEFAULT 0);
Explanation
r2dbc-mysql 1.3.0
, BIT(1)
columns are natively supported and correctly mapped to Boolean
fields in Java entities. (https://github.com/asyncer-io/r2dbc-mysql/releases/tag/r2dbc-mysql-1.3.0)TINYINT(1)
type is not treated as a boolean
by default because r2dbc-mysql currently does not implement the tinyInt1isBit
flag. This behavior will be updated in a future release to better align with how TINYINT(1)
is commonly used in MySQL.