Problem
These error occurs when selecting columns from the database that don’t match the model, for example:
1 | org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'name' from result set. Cause: java.sql.SQLDataException: Cannot determine value type from string 'singming' |
1 | org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'type' from result set. Cause: java.sql.SQLDataException: Cannot convert string 'MALE' to java.sql.Timestamp value |
Solution
Ensure your SQL query only selects columns that exist in your Java model to avoid type mismatches and data integrity issues.
Explanation
The error looks ridiculous, it is because mybatis is trying to map results from sql to some fields in the java model. e.g. mapping a name VARCHAR(255)
to Date createdTime
even if you add the jdbcType
mapping in the <resultMap>
MyBatis expects a direct mapping between columns in your SQL query and properties in your Java model. If your SQL query selects columns that don’t exist in your model, you can encounter type mismatches errors above.
To resolve this:
Review your SQL queries: Ensure that you only select columns that have corresponding properties in your Java model.
Use aliases: If your SQL query retrieves columns with different names but equivalent data, use aliases to match them to the model properties.
Example
1 | <!-- MyBatis XML Mapper --> |
1 | // Java model |
In this example, the SQL query and Java model are aligned, resulting in a smooth mapping without type mismatches.