Hibernate的使用总结

简介: 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

 

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
传感器 机器学习/深度学习 数据采集
2022年第十一届认证杯数学中国数学建模国际赛小美赛:C 题 对人类活动进行分类 建模方案及代码实现
本文提供了2022年第十一届认证杯数学中国数学建模国际赛小美赛C题"对人类活动进行分类"的建模方案和Python代码实现,包括数据预处理、特征提取、LSTM网络模型构建和训练评估过程。
661 11
2022年第十一届认证杯数学中国数学建模国际赛小美赛:C 题 对人类活动进行分类 建模方案及代码实现
|
Android开发 iOS开发
关于android 字体设置为中等粗细
Android TextView设置字体粗细只有三种状态,textStyle取值只有bold、nomral、italic、客户需求是字体是medium粗细,只因ios是有medium属性,Android被要求要一样;
2099 0
关于android 字体设置为中等粗细
|
算法 C++ Python
算法笔记(1)—— 搜索算法:线性搜索(LS)、二分搜索(BS)、记忆化搜索(MS)
算法笔记(1)—— 搜索算法:线性搜索(LS)、二分搜索(BS)、记忆化搜索(MS)
4418 0
|
Ubuntu Linux
Ubuntu文件系统损坏修复
前两天在使用Ubuntu14.04系统时,不知道何种原因桌面系统没有任何反应(ps:可能是开机运行的时间比较长吧),所以就对其进行了强制关机操作,可是悲剧的事情发生了,系统无法启动了,并且进入initramfs系统
1674 0
|
Linux 程序员 API
深入理解linux互斥锁(mutex)
锁机制,可以说是linux整个系统的精髓所在,linux内核都是围绕着同步在运转。在多进程和多线程编程中,锁起着极其重要的作用。
6290 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8057 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18273 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置