Loading...
My Blog 我的工作和学习笔记

Hibernate基础知识汇总

Hibernate专题 2015/12/29 Hibernate

一、Hibernate缓存机制

hibernate缓存分为会话缓存(session)和工厂缓存(sessionFactory),即一级缓存和二级缓存;一级缓存是session级别的,是私有缓存,在session里面根据ID获取实体;二级缓存是sessionFactory级别的,即服务应用级别,是共享缓存,也是根据ID来获取实体;如果想将list也缓存至二级缓存,则需要开启查询缓存,查询缓存是缓存相同sql语句的list信息,即只有sql语句相同的才会去读取查询缓存,如果查询信息多变的话,缓存命中率比较低,所以一般不开启查询缓存。

参考文档如下:

http://blog.csdn.net/dongnan591172113/article/details/7943652

http://www.blogjava.net/tbwshc/articles/380013.html


二、Hibernate get和load区别

get和load的根本区别:

hibernate对于load方法认为该数据在数据库中一定存在,可以放心的使用代理来延迟加载,如果在使用过程中发现了问题,只能抛org.hibernate.ObjectNotFoundException异常;

而对于get方法,hibernate一定要获取到真实的数据,否则返回null。

http://www.cnblogs.com/binjoo/articles/1621254.html

 

三、Hibernate 级联cascade和关系维持inverse

http://418684644-qq-com.iteye.com/blog/1199068



Hibernate hql 查询语句

Hibernate专题 2015/12/18 Hibernate

#many to many 

实体:User(List<Application> applicationList),Application

select u from User u inner join u.applicationList a where a.id=:applicationId;


#like

from User u where u.username like '%param%';


未完待续~


参考文档

http://www.cnblogs.com/bobomail/archive/2005/09/20/240352.html


数据库长时间没连接后的异常处理[org.hibernate.engine.jdbc.spi.SqlExceptionHelper] -

Hibernate专题 2015/03/30

2014-12-31 01:16:48,258 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] -

2014-12-31 01:16:48,258 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] -


原因:You're probably running into the fact that MYSQL closes connections   
which have been open "too long". 
Probably if you make the request to the server again, you'll find that   
it works because the connection is reopened. 


解决方法:One way to deal with this is to add the following to your context.xml   
Resource: 

validationQuery="select 1"
 

代价:This causes a very cheap test query to always be run first; if the   
connection has been closed, this gets the failure, and 
then a new connection is opened