hibernate映射xml文件报错:found [text (Types#LONGVARCHAR)], but expecting [varchar(255) (Types#VARCHAR)]

December 12, 2016

问题:

found [text (Types#LONGVARCHAR)], but expecting [varchar(255) (Types#VARCHAR)]
描述:

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [content] in table [wos_refer.record]; found [text (Types#LONGVARCHAR)], but expecting [varchar(255) (Types#VARCHAR)]

源码:
Record.java
package refer.model.po;

/**
 * Created by lv on 2016/12/11.
 */
public class Record {
    private int id;
    private String content;
    private Integer paperId;
    private Integer reportId;

    public Record(String content, Integer paperId, Integer reportId) {
        this.content = content;
        this.paperId = paperId;
        this.reportId = reportId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Integer getPaperId() {
        return paperId;
    }

    public void setPaperId(Integer paperId) {
        this.paperId = paperId;
    }

    public Integer getReportId() {
        return reportId;
    }

    public void setReportId(Integer reportId) {
        this.reportId = reportId;
    }
}
Record.hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="refer.model.po.Record" table="record" schema="wos_refer">
        <id name="id" column="id"/>
        <property name="content" column="content"/>
        <property name="paperId" column="paper_id"/>
        <property name="reportId" column="report_id"/>
    </class>
</hibernate-mapping>
mysql
CREATE TABLE `record` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` text CHARACTER SET utf8,
  `paper_id` int(11) DEFAULT NULL,
  `report_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

原因:

mysql中content为text类型,hibernate映射文件中没有特殊标注默认String对象对应mysql中的VARCHAR类型,所以报如上错误。

解决方法:

<property name="content" column="content" type="text"/>
映射xml文件content字段添加type="text"属性。