org.hibernate.HibernateException: Wrong column type
这个问题一般出现在我们使用定长的字符串作为主键(其它字段也可能)的时候,如数据库中的ID为char(16)。虽然很多资料上都说不推荐这样做,但实际上我们在做很多小case的时候自己为了方便也顾不得那么多,随心所欲地设计。其实我们就用INT的主键,只是对你原有的ID(char(16))增加一个Unique Check或者是像在MySQL中增加一个Unique索引又费得了多少事呢。
如果使用char()做为主键时出现如题错误,其很可能的原因在于你的hibernate.cfg.xml文件中的关于Hibernate映射到数据定义语言(DDL)的配置
- <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表格
- CREATE TABLE USER (
- user_id CHAR(32) NOT NULL PRIMARY KEY,
- name VARCHAR(16) NOT NULL,
- sex CHAR(1),
- age INT
- );
Java类User.java
- package onlyfun.caterpillar;
- public class User {
- private String id;
- private String name;
- private char sex;
- private int age;
- public int getAge() {
- return age;
- }
- public String getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public char getSex() {
- return sex;
- }
- public void setAge(int i) {
- age = i;
- }
- public void setId(String string) {
- id = string;
- }
- public void setName(String string) {
- name = string;
- }
- public void setSex(char c) {
- sex = c;
- }
- }
User.hbm.xml文件配置
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping
- PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
- <hibernate-mapping>
- <class name="onlyfun.caterpillar.User" table="USER">
- <id name="id" type="string" unsaved-value="null">
- <column name="user_id" sql-type="char(32)" />
- <generator class="uuid.hex"/>
- </id>
- <property name="name" type="string" not-null="true">
- <column name="name" length="16" not-null="true"/>
- </property>
- <property name="sex" type="char"/>
- <property name="age" type="int"/>
- </class>
- </hibernate-mapping>
hibernate.cfg.xml文件配置
- <hibernate-configuration>
- <session-factory>
- <property name="show_sql">true</property>
- <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost/HibernateTest</property>
- <property name="connection.username">caterpillar</property>
- <property name="connection.password">123456</property>
- <mapping resource="User.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
上面只是个简单的配置实例,主要在于展示User的char(32)类型的ID如何配置。
刚接触Hibernate,有些简单的问题也会搞得你晕头转向,以此作为自己的学习笔记,欢迎指导!
org.hibernate.HibernateException: Wrong column type的更多相关文章
- org.hibernate.HibernateException: No Session found for current thread
spring.springmvc和hibernate整合 在sessionFactory.getCurrentSession()时,出现以下异常 No Session found for curren ...
- 报错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; ...
- spring整合hibernate的时候报异常org.hibernate.HibernateException: createQuery is not valid without active transaction
在整合Spring4+hibernate4时候,当代码执行到dao中CRUD操作时,报了一个异常, org.hibernate.HibernateException: createQuery is n ...
- 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 ...
- 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" ...
- 问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found解决方法
问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not fo ...
- 使用hibernate时出现 org.hibernate.HibernateException: Unable to get the default Bean Validation factory
hibernate 在使用junit测试报错: org.hibernate.HibernateException: Unable to get the default Bean Validation ...
- 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 ...
- org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml at org.hibernate ...
随机推荐
- SWFUpload 在ie9上出现的bug
SWFUpload 在ie9下会出现js错误 参考以下几个网址,备忘: http://code.google.com/p/swfupload/issues/detail?id=348 http://c ...
- C# 利用VS中的插件来打包并发布winfrom程序
1.先在VS 的扩展更新中搜索此插件[2015 installer Projects],点击下载,安装需要关闭VS 2.安装完毕之后新建项目 3.选择“application folder”项,然后在 ...
- Android逆向进阶—— 脱壳的奥义(基ART模式下的dump)
本文作者:i春秋作家HAI_ZHU 0×00 前言 市面上的资料大多都是基于Dalvik模式的dump,所以这此准备搞一个ART模式下的dump.HAI_的使用手册(各种好东西) Dalvik模式是A ...
- [ActionScript 3.0] as3处理xml的功能和遍历节点
as3比as2处理xml的功能增强了N倍,获取或遍历节点非常之方便,类似于json对像的处理方式. XML 的一个强大功能是它能够通过文本字符的线性字符串提供复杂的嵌套数据.将数据加载到 XML 对象 ...
- 转载:TCP/IP四层模型
转载:TCP/IP四层模型 一. TCP/IP参考模型示意图 ISO制定的OSI参考模型的过于庞大.复杂招致了许多批评.与此对照,由技术人员自己开发的TCP/IP协议栈获得了更为广泛的应用. 如图所示 ...
- MySQL查询50例
创建表和关系 /* 创建表 */ /*年级表*/ DROP TABLE IF EXISTS `class_grade`; CREATE TABLE `class_grade` ( `gid` int( ...
- 【xsy2818】 最近点 动态树分治+可持久化线段树
题目大意:给你一颗n个节点的树,最初点集S为空. 有m次操作:往当前点集S中加入/删除一个点,询问点x至集合S中任意点的最小距离,回到第t次修改点集的操作后的状态. 数据范围:$n,m≤10^5$ 我 ...
- macbook 安装oracle RAC
http://blog.itpub.net/29047826/viewspace-1268923/ http://blog.itpub.net/24930246/viewspace-1426856/
- atexit()使用
mian()主函数执行完毕后,是否可能会再执行一段代码?如果需要加入一段代码在mian退出后执行的代码,可以使用atexit()函数注册一个函数,代码如下: #include <iostream ...
- logstash安装与logstash-input-jdbc插件使用
ElasticSearch的索引可以手动添加索引的,就是类似下面这样添加的 PUT /movies/movie/1 { "title": "The Godfather&q ...