如何使Hibernate将CLOB转换为MySQL类型

时间:2014-02-18 03:59:48

标签: java mysql hibernate clob

在我的代码中,实体类的字段具有CLOB列。这适用于Oracle,但当我尝试使用MySQL作为数据库时,它看起来CLOB类型导致问题,因为MySQL不支持CLOB。控制台错误是

13:35:27 ERROR [main] org.hibernate.tool.hbm2ddl.SchemaUpdate          您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'CLOB附近使用正确的语法,         messageSubject longtext,         senderEmailAddress varchar(255), '在第7行[主要]

java代码片段是:

@Column(length = 4000)
private String messageSubject;

@Column(columnDefinition = "CLOB")
private String messageBody;

这是我的entityfactory

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="runtime" />
    <property name="dataSource">
        <bean class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="jdbc/cggruntime" />
        </bean>
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
            <property name="generateDdl" value="true" />
            <property name="showSql" value="false" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.jdbc.batch_size">10</prop>
            <prop key="hibernate.archive.autodetection">class</prop>        

            <!-- Activate Second-Order Cache for Entities and Queries -->
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            <prop key="hibernate.cache.use_query_cache">false</prop>
        </props>
    </property>
</bean>

我的问题是有没有办法配置hibernate,以便CLOB可以自动转换为MySQL支持的其他数据类型(如Text)?

感谢

1 个答案:

答案 0 :(得分:1)

MySQL绝对支持CLOB。将Java String转换为MySQL CLOB的正确语法如下:

@Lob
@Column(name = "columnNameForTheMysqlColumnHere")
private String messageBody;