Hibernate + MySQL自定义布尔类型

时间:2014-12-18 07:20:13

标签: spring hibernate boolean pojo hibernate-tools

Hibernate工具在javacode中生成tinyint(1)到布尔类型。默认值:0 false,1 true

但我想改变1:假和0:真。

我怎么做?

我在我的datasource.xml中添加了<prop key="hibernate.query.substitutions">true 0, false 1</prop>但它仍然不起作用。

<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>

请帮帮我,谢谢!

1 个答案:

答案 0 :(得分:0)

如下所示更改您的财产并检查

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

您的代码应该像

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

此外,在您的INFO级别日志中,您可以看到类似这样的内容

  

INFO SettingsFactory:203 - 查询语言替换:{true = 0,   假= 1}

在旧版本的hibernate(https://hibernate.atlassian.net/browse/HHH-165)中似乎有一些query.substitutions的问题,请在较新版本中查看相同内容。