Reputation: 45
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:
Upvotes: 1
Views: 76
Reputation: 36
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)
or ‘TINYINT(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)Upvotes: 1
Reputation: 11
You can try to change the field type in the database to tinyint so that spring data can do the mapping directly
Upvotes: 1