Hibernate的使用总结

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

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

I recently launched a new site based on StrutsHibernate 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 same problem, 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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值