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 ...
随机推荐
- js gettext
test.php 1 <?php $locale='zh_CN'; if(isSet($_GET["locale"]))$locale = $_GET["local ...
- 使用JAVA API 解析ORC File
使用JAVA API 解析ORC File orc File 的解析过程中,使用FileInputFormat的getSplits(conf, 1)函数, 然后使用 RecordReaderreade ...
- ASP.NET自定义错误页并返回正确的500、404状态码
在项目中,我们常常需要自定义错误页面,但往往返回的状态码都变成了200,对SEO很不友好.我尝试过在百度上寻找解决方案,但找到的资料中说的方法都试过了,发现都是无法返回正确的状态码的. 最后,只好自已 ...
- 混合表单文件上传到数据库(基于TOMCAT)
在实际的开发中在实现文件上传的同时肯定还有其他信息需要保存到数据库,就像混合表单在上传完毕之后需要将提交的基本信息插入数据库. 在这个demo中需要用到这个架包来帮助实现 1.定义一个公共类实现文件上 ...
- MariaDB 连接查询与子查询(6)
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...
- 【hyperscan】hyperscan开源了!
hyperscan开源了! 官网:https://01.org/zh/hyperscan 1. 新闻背景 当地时间10月19日,intel将它的高速正则表达式匹配引擎hyperscan开源了,版本4. ...
- cmd下【java监视和管理控制台】
不需要安装插件,只要jmeter的运行环境配置好就可以了:打开这个小工具的步骤很简单,如果你已经配置好了Jmeter运行的环境,那么你也就不用去做其他的配置,直接 点击:开始——>运行——> ...
- VS2013 编辑器
1. VS -> 本地Git -> Github 1. 右键单击解决方案,选择“将解决方案添加到源代码管理器”,选择Git 2. 切换到团队资源管理器([菜单]视图->团队资源管理器 ...
- postgresql-int,bigint,numeric效率测试
在postgresql9.5的时候做过一个测试就是sum()的效率最终的测试结果是sum(int)>sum(numeric)>sum(bigint)当时比较诧异为啥sum(bigint)效 ...
- centos7 完整配置openvpn详情教程
1. 什么是OpenVpn OpenVPN 是一个用于创建虚拟专用网络加密通道的软件包,最早是由James Yonan编写的.OpenVPN允许创建的VPN使用公开密钥.电子证书.或者用户名/密码来进 ...