这个问题一般出现在我们使用定长的字符串作为主键(其它字段也可能)的时候,如数据库中的ID为char(16)。虽然很多资料上都说不推荐这样做,但实际上我们在做很多小case的时候自己为了方便也顾不得那么多,随心所欲地设计。其实我们就用INT的主键,只是对你原有的ID(char(16))增加一个Unique Check或者是像在MySQL中增加一个Unique索引又费得了多少事呢。

如果使用char()做为主键时出现如题错误,其很可能的原因在于你的hibernate.cfg.xml文件中的关于Hibernate映射到数据定义语言(DDL)的配置

  1. <property name="hbm2ddl.auto">validate</property>

当hbm2ddl设置为validate,每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。validate的具体内部实现我不清楚,但我想正是因为每次验证比较导致了如题问题的出现。数据库里字段类型为char(),而你的对象属性为java.lang.String,出现了错误的列类型。

这样的错误并不是经常出现,原因在于我们配置hibernate.cfg.xml文件的时候一般不配置hbm2ddl这一项,即使用默认值“update”,而且在开发或学习的过程中我们通常会配置为“create”,也就很难遇到这样的错误。到这里解决办法已经很明确了,即更改你的hbm2ddl配置。

下面给出一个使用char()做主键的配置实例(源自:Dashboard(Hibernate入门)):

mysql中新增一個HibernateTest資料庫,並建立USER表格

  1. CREATE TABLE USER (
  2. user_id CHAR(32) NOT NULL PRIMARY KEY,
  3. name VARCHAR(16) NOT NULL,
  4. sex CHAR(1),
  5. age INT
  6. );

Java类User.java

  1. package onlyfun.caterpillar;
  2. public class User {
  3. private String id;
  4. private String name;
  5. private char sex;
  6. private int age;
  7. public int getAge() {
  8. return age;
  9. }
  10. public String getId() {
  11. return id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public char getSex() {
  17. return sex;
  18. }
  19. public void setAge(int i) {
  20. age = i;
  21. }
  22. public void setId(String string) {
  23. id = string;
  24. }
  25. public void setName(String string) {
  26. name = string;
  27. }
  28. public void setSex(char c) {
  29. sex = c;
  30. }
  31. }

User.hbm.xml文件配置

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping
  3. PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
  5. <hibernate-mapping>
  6. <class name="onlyfun.caterpillar.User" table="USER">
  7. <id name="id" type="string" unsaved-value="null">
  8. <column name="user_id" sql-type="char(32)" />
  9. <generator class="uuid.hex"/>
  10. </id>
  11. <property name="name" type="string" not-null="true">
  12. <column name="name" length="16" not-null="true"/>
  13. </property>
  14. <property name="sex" type="char"/>
  15. <property name="age" type="int"/>
  16. </class>
  17. </hibernate-mapping>

hibernate.cfg.xml文件配置

  1. <hibernate-configuration>
  2. <session-factory>
  3. <property name="show_sql">true</property>
  4. <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
  5. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  6. <property name="connection.url">jdbc:mysql://localhost/HibernateTest</property>
  7. <property name="connection.username">caterpillar</property>
  8. <property name="connection.password">123456</property>
  9. <mapping resource="User.hbm.xml"/>
  10. </session-factory>
  11. </hibernate-configuration>

上面只是个简单的配置实例,主要在于展示User的char(32)类型的ID如何配置。

刚接触Hibernate,有些简单的问题也会搞得你晕头转向,以此作为自己的学习笔记,欢迎指导!

org.hibernate.HibernateException: Wrong column type的更多相关文章

  1. org.hibernate.HibernateException: No Session found for current thread

    spring.springmvc和hibernate整合 在sessionFactory.getCurrentSession()时,出现以下异常 No Session found for curren ...

  2. 报错HTTP Status 500 - HHH000142: Javassist Enhancement failed: cn.itcast.entity.Customer; nested exception is org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: cn.itcast.entity.

    报错 type Exception report message HHH000142: Javassist Enhancement failed: cn.itcast.entity.Customer; ...

  3. spring整合hibernate的时候报异常org.hibernate.HibernateException: createQuery is not valid without active transaction

    在整合Spring4+hibernate4时候,当代码执行到dao中CRUD操作时,报了一个异常, org.hibernate.HibernateException: createQuery is n ...

  4. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a ...

  5. org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:

    详细错误堆栈信息: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" ...

  6. 问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found解决方法

    问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not fo ...

  7. 使用hibernate时出现 org.hibernate.HibernateException: Unable to get the default Bean Validation factory

    hibernate 在使用junit测试报错: org.hibernate.HibernateException: Unable to get the default Bean Validation ...

  8. Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set

    docs.jboss.org文档示例代码:(http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/) sta ...

  9. org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml

    org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml at org.hibernate ...

随机推荐

  1. SQLSqlserver中如何将一列数据,不重复的拼接成一个字符串

    把一列数据拼接成一个字符串比较简单: declare @test varchar(500) set @test=''; select @test=@test+name+',' from person ...

  2. 【.NET架构】BIM软件架构02:Web管控平台后台架构

    一.前言        之前一篇叙述的是Revit插件(桌面软件)的软件架构,本篇将开始叙述Web项目的架构方案.今年一月在老东家加入BIM平台部门,为一些大型国家项目搭建BIM管控平台,业主使用管控 ...

  3. 数据导出之winfrom导出word(二)

    本篇文章介绍了根据word模板导出word文档的方法. 一.获取模板地址 WordDocFileHelper WordTem = new WordDocFileHelper(); string pat ...

  4. 【新手向】阿里云上ubuntu+flask+gunicorn+nginx服务器部署(二)项目部署

    本项目实现的是类似于ins的图片分享网站.继续(一),当nginx的配置已修改好后,要在远程服务器上部署网站,只需要几个步骤: 1 前期准备 2 将运行网站的代码从github上下载过来 3 下载依赖 ...

  5. 解压cpio.gz

    #gunzip 文件名.cpio.gz #cpio -idmv < 文件名.cpio

  6. web socket 入门

    WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议,其优雅地解决了以往web服务器不能向web客户端实时推送消息的问题. 在浏览器js环境中,创建一个websock ...

  7. Django设置联合唯一约束 -- migrate时报错处理

    异常信息: a unique database constraint for 2 or more fields together 场景描述: 对于ORM中多对多关系的中间表,如果该关系表是手动创建的, ...

  8. 在MVC3中使用富文本编辑器:KindEditor的配置及上传图片

    现在比较常用的富文本编辑挺多的,如ueditor.fckeditor.kingeditor等,本文主要介绍一下KindEditor的配置与使用. 先去官网http://www.kindsoft.net ...

  9. 微信小程序交流群,欢迎加入,其中微信小程序开发群、Jenkins开发群是有问必答群

    微信小程序开发,请加群511389428,511389428 有问必答群:React开发,请加群523838207:523838207Jenkins开发,请加群155799363,155799363  ...

  10. web的脚本安全-CSRF

    CSRF,即Cross-site request forgery,中文一般叫跨站请求伪造. 攻击原理是,用户在A网站(登录,之后打开一个B网站,B网站的脚本(或HTML标签)向A网站发送一个请求,这个 ...