Hibernate + MySQL custom boolean type

676 Views Asked by At

Hibernate tool generate tinyint(1) to boolean type in javacode. with default value: 0 false, 1 true

But i want to change 1: false and 0: true.

How i can do it?

I have added <prop key="hibernate.query.substitutions">true 0, false 1</prop> in my datasource.xml but it still not work.

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <!-- Read database connection from data source -->
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <!-- Config hibernate properties -->
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.format_sql">true</prop>
            <!-- Minimum number of JDBC connections in the pool. Hibernate default: 1 -->
            <prop key="hibernate.c3p0.min_size">5</prop>
            <!-- Maximum number of JDBC connections in the pool. Hibernate default: 100 -->
            <prop key="hibernate.c3p0.max_size">20</prop>
            <!-- When an idle connection is removed from the pool (in second). Hibernate default: 0, never expire. -->
            <prop key="hibernate.c3p0.timeout">300</prop>
            <!-- Number of prepared statements will be cached. Increase performance. Hibernate default: 0 , caching is disable. -->
            <prop key="hibernate.c3p0.max_statements">50</prop>
            <!-- Hibernate.c3p0.idle_test_period – idle time in seconds before a connection is automatically validated. Hibernate default: 0 -->
            <prop key="hibernate.c3p0.idle_test_period">3000</prop>
            <prop key="hibernate.query.substitutions">true 0, false 1</prop>
        </props>
    </property>

Please help me out, thank you!

1

There are 1 best solutions below

0
On

Change your property as below and check

<property name="hibernate.query.substitutions" value="true 0, false 1" />

Your code should be like

@Column(name = “is_active”, columnDefinition=”TINYINT(1)”)
private boolean isActive;

Also In your INFO level logs you can see some thing like this

INFO SettingsFactory:203 - Query language substitutions: {true=0, false=1}

It seems there are some issues with query.substitutions in older versions of hibernate(https://hibernate.atlassian.net/browse/HHH-165) ,please check same in newer versions.