Hibernate的使用总结

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: Hibernate的使用总结

1.把mysql的关键字interval ,time当做了字段描述而引起的。

2.和数据库连接经常断开,Hibernate: Communication link failure: java.io.IOException

I recently launched a new site based on Struts, Hibernate and MySQL and immediately ran into a weird issue where Hibernate lost the ability to make database connections after a long period of inactivity. For the record, the stack trace is below:


java.sql.SQLException: Communication link failure: java.io.IOException
 at org.gjt.mm.mysql.MysqlIO.sendCommand(Unknown Source)
 at org.gjt.mm.mysql.MysqlIO.sqlQueryDirect(Unknown Source)
 at org.gjt.mm.mysql.Connection.execSQL(Unknown Source)
 at org.gjt.mm.mysql.PreparedStatement.executeQuery(Unknown Source)
 at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:83)
 at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:794)
 at net.sf.hibernate.hql.QueryTranslator.iterate(QueryTranslator.java:846)
 at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1540)
 at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1513)
 at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1505)


Looks like there are other people having the sameproblem, the first suggestion was to use the Thread Local Session pattern, which I already had in place. The solution was to add the following properties to my hibernate.cfg.xml:


<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>


which I believe are specific to MySQL, but as far as I can tell, aren't documented anywhere on the Hibernate site (or should they be documented on the MySQL JDBC driver site?)

For what it's worth, Hibernate is a dream come true. I don't like writing create, update, and delete SQL statements and I've found that the software I've written is much easier to manage and troubleshoot. If you haven't played with it yet, check it out now.

也可以写成

<prop key="hibernate.connection.autoReconnect">${hibernate.connection.autoReconnect}</prop>

形式

3.org.hibernate.MappingException: Unknown entity

原因1: import org.hibernate.annotations.Entity;需要改成 import javax.persistence.Entity;

原因2:外有些报org.hibernate.MappingException: Unknown entity,可能是没有在cfg文件中加入 *.hbm.xml造成的

原因3:感觉是包的层次放的太深造成的

4.Cannot convert value '0000-00-00 00:00:00' from column 9 to TIMESTAMP

把时间从'0001-01-01 00:00:00'改成'2009-05-18 01:02:03'就OK了,网上介绍如下:

查看了mysql5的帮助文档对于datetime的解释如下

Datetimes with all-zero components (0000-00-00 ...) — These values can not be represented 关于所有Datetime类型由0组成的数据,这些值不能在java中被可靠的表示
reliably in Java.
Connector/J 3.0.x always converted them to NULL when being read from a ResultSet.
当这些值正在从ResultSet容器中读取时候,Connector/J 3.0.x 一直把他们转换为NULL值。

Connector/J 3.1 throws an exception by default when these values are encountered as this is the most correct behavior according to the JDBC and SQL standards.
依照JDBC和SQL的标准这些值碰到的最正确的处理方式就是在缺省情况下产生异常
This behavior can be modified using the zeroDateTimeBehavior configuration property. The allowable values are:
JDBC允许用下列的值对zeroDateTimeBehavior 属性来设置这些处理方式,

exception (the default), which throws an SQLException with an SQLState of S1009.
设置为exception 异常(缺省)用一个SQLState的s1009错误号来抛出一个异常
convertToNull, which returns NULL instead of the date.
设置为convertToNull,用NULL值来代替这个日期类型
round, which rounds the date to the nearest closest value which is 0001-01-01.
设置为round,则围绕这个日期最接近的值(0001-01-01)来代替

你可以修改你的jdbc连接

jdbc:mysql://localhost/schoolmis?useUnicode=true&amp;characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull

但我用了一下却没什么效果

Row was updated or deleted by another transaction

2009-03-16 20:38

 

Row was updated or deleted by another transaction

2009-03-16 20:38

遇到这个问题了。连续刷新同一个页面就会出现问题,数据库锁死。没有办法继续相关的操作。一直不知道怎么解决。网上搜索,发现是Hibernate中的Timestamp乐观锁死功能抛出的异常。大意就是在数据表中设立一个时间字段Timestamp用来表示数据的版本。当该条数据的时候这个timestampe不能为空,而且在javabean中不能调用setTimestampe()的方法。如果这样做了,就会抛出Row was updated or deleted by another transaction 这个异常了。

      但是我自己没有特意设定一个时间字段Timestamp,怎么会有这个问题。反复看了若干遍自己的代码。发现我的数据表在通过Myeclipses生成Hibernate的数据表映射文件是,自动的将表中一个叫version的字段映射成为用于version标识的参数了,而不是应该数据表的一个字段。我的程序中连续刷新某个页面的时候,相当于触动更新一个特定的数据表。连续刷新时就是连续更新一个表里的同一个字段,就出现问题了。将生成的配置文件xxxxx.xml(xxxxx代表数据表的名称)中的

<version name="version" type="java.lang.Integer" column="version" />
删除就ok了
。 看来数据表起名的时候还要特别注意啊。以前就犯过这个问题。数据库我还要加强。。。

当然,既然提到这个问题,就将hibernate的这个功能说一下。还是偷懒 直接zz,为表示尊重,可以直接去下面的链接去看作者原文。

hibernate.hbm2ddl.auto=update不能自动生成表结构原因排查

1.发现其他表的新增字段能正常增加,但新建表却失败了。

查看hibernate的日志,

ERROR: HHH000388: Unsuccessful: create table activity_fighter_puzzle_two
createTime bigint not null, initDay integer not null,
invalid bit not null,playerId varchar(255) not null,score integer not null,
termId integer not null,updateTime bigint not null,primary key (id))
ERROR: Specified key was too long; max key length is 767 bytes

如果在一个utf8mb4字符集、长度为varchar(255)字段上建索引,则其所占长度为255*4字节,大于767,将导致报错。innodb引擎 默认索引长度767 bytes

所以导致创建表失败了。

2.创建表的时候加了hibernate加的length超出了长度:

ERROR: Column length too big for column 'content'(max = 21845);use BLOB or TEXT instead

 

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
11月前
|
SQL 存储 Java
Hibernate
Hibernate
48 0
|
SQL Java 关系型数据库
|
SQL Java 数据库连接
|
关系型数据库 Java 数据库连接
|
SQL Java 数据库连接
Hibernate初识
简介 相信学过SSH2框架的同学们对Hiberate一点也不陌生。Hibernate是Gavin King于2001年开始,作为EJB2风格实体bean的替代品开发的对象关系映射工具。
1166 0