日期:2014-05-17  浏览次数:20931 次

使用Hibernate往MySql的LONGTEXT字段中存储长文本该如何设置?
现需要将大段文本插入数据库中,hbm.xml中设置如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ibm.heyang.domain">
<class name="Wisdom" table="collections_wisdom">
<id name="id" column="ID" >
<generator class="increment"/>
</id>

<property name="title" column="title" not-null="true"/>
<property name="concept" type="text">
<column name="concept" not-null="true" sql-type="LONGTEXT"/>
</property>

<property name="addUserId" column="addUserId" />
<property name="modifyTime" column="modifyTime"/>
</class>
</hibernate-mapping>

存储时,当concept字段是短文本时没有问题,为稍长文本就出现下面的错误:
Caused by: java.sql.BatchUpdateException: Data truncation: Data too long for column 'concept' at row 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1237)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:936)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 41 more

请问我该如何调整? 

------解决方案--------------------
<property name="concept" type="text">
<column name="concept" not-null="true" length = "16777216" />
</property>

这样改就好了.