MyBatis - jdbcTypeForNull Oracle
Asked Answered
D

5

12

I am using MyBatis with an Oracle 11g R2 database. I am using MyBatis 3.3 with ojdbc6 12.1.0.2. My issue is whenever I tried to insert an object that is null I get the following.

org.springframework.jdbc.UncategorizedSQLException: Error setting null for parameter #8 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: Invalid column type: 1111

My understanding is in the latest version of JDBC null is mapped to JdbcType.OTHERS which no all drivers handle, apparently Oracle is one of them.

I tried the following in my MyBatis configuration, but still no luck.

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setTypeAliasesPackage("org.ohtech.innovationexchange.core.domain");
        sessionFactory.setTransactionFactory(springManagedTransactionFactory());
        sessionFactory.setConfigurationProperties(getProperties());
        return sessionFactory.getObject();
    }

    @Bean(name = "transactionManager")
    public DataSourceTransactionManager dataSourceTransactionManager() throws PropertyVetoException{
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public SpringManagedTransactionFactory springManagedTransactionFactory() {
        return new SpringManagedTransactionFactory();
    }

    private Properties getProperties() {
        final Properties myBatisProperties = new Properties();
        myBatisProperties.put("jdbcTypeForNull", "NULL");
        return myBatisProperties;
    }

I can make it work by doing the following in my mapper files but it seems really repetitive and ugly. Not sure why MyBatis is not using my properties I am passing the SqlSessionFactory bean.

Diplopod answered 18/8, 2015 at 19:40 Comment(1)
possible duplicate of MyBatis ORA-01745: invalid host/bind variable nameBatory
T
8

"jdbcTypeForNull" is not a 'property' but a 'setting'. Can not be set by java config currently, I guess. You need a config.xml like this:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="jdbcTypeForNull" value="NULL" />
    </settings>
</configuration>

and use sessionFactory.setConfigLocation(...).

For the difference between settings and properties please refer to the documentation: https://mybatis.github.io/mybatis-3/configuration.html

Tegantegmen answered 23/9, 2015 at 14:28 Comment(0)
M
4

Maybe its late, but here is the solution:

@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

    sessionFactory.setDataSource(dataSource);

    sessionFactory.setTypeAliasesPackage("com.xxx.mapper");

    SqlSessionFactory sqlSessionFactory = sessionFactory.getObject();

    sqlSessionFactory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);

    return sqlSessionFactory;
}
Misread answered 3/11, 2016 at 23:10 Comment(0)
A
4

For those using the mybatis Spring Boot starter, this is the property you can add to application.properties to correct this problem:

mybatis.configuration.jdbc-type-for-null=NULL
Acutance answered 2/5, 2019 at 15:51 Comment(0)
M
2

If you are using mybatis spring boot starter and have application.yml then you need to add this property.

mybatis:
  configuration:
    jdbc-type-for-null: "NULL"
Miscreated answered 19/10, 2021 at 10:12 Comment(1)
YAML configuration really needs quotes around NULL. Thank you @pratiyush.Pomade
B
0

If you are using spring mybatis combination, refer to the new spring config file like this:

<bean id="myAppSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" name="myAppSqlSessionFactory">
    <property name="dataSource" ref="myAppDataSource" />
    <property name="typeAliasesPackage" value="com.myapp.model" />
    <property name="configLocation" value="mybatis-config.xml"/>
</bean>
Becky answered 16/4, 2018 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.