存档

文章标签 ‘Hibernate’

org.hibernate.SessionException: Session is closed! 异常解决方法

2010年4月29日 IT北瓜 6 条评论

刚刚在程序中出现了org.hibernate.SessionException: Session is closed! 这个异常,查看Firebug控制台,还能发现有这样的异常信息org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction;记录一下解决方法。

程序代码如下:

public boolean activityUpdate(Activity activity) throws Exception {

	Session session = null;
	Transaction tran = null;

	boolean r = false;

	try{
		//创建sql
		String sql1 = "delete from Activityfilm where activityid=" + activity.getId();//删除活动影片数据
		String sql2 = "delete from Activityclient where activityid=" + activity.getId();//删除活动客户数据

		//获得session
		session = this.getSession();
		//开始事务
		tran = session.beginTransaction();
		//执行删除操作
		int del_1 = session.createSQLQuery(sql1).executeUpdate(), del_2 = session.createSQLQuery(sql2).executeUpdate();
		System.out.println("del_1: "+ del_1 + "###del_2: " + del_2);
		//更新活动
		session.update(activity);
		if(del_1 > 0 && del_2 > 0){
			r = true;
		}else{
			r = false;
		}
		System.out.println("r: "+ r);
		tran.commit();
		if(!r){//执行回滚
			System.out.println("执行回滚#####################");
			tran.rollback();
		}
	}catch(Exception ex){
		if(tran != null){
			System.out.println("执行回滚@@@@@@@@@@@@@@@@@@@@@");
			tran.rollback();
		}
		ex.printStackTrace();
	}finally{
		if(tran != null){
			System.out.println("设置事务为null");
			tran = null;
		}
		if(session != null){
			System.out.println("关闭session$$$$$$$$$$$$$$$$$");
			session.close();//执行这一行总是报异常:org.hibernate.SessionException: Session is closed! 
		}
	}
	System.out.println("返回: "+ r);
	return r;

}

引用网上的资料:

先看看SessionFactory.getCurrentSession与openSession的区别

1. 如果使用的是getCurrentSession来创建session的话,在commit后,session就自动被关闭了,

    也就是不用再session.close()了。但是如果使用的是openSession方法创建的session的话,

    那么必须显示的关闭session,也就是调用session.close()方法。这样commit后,session并没有关闭

/*2. getCurrentSession的使用可以参见hibernate\hibernate-3.2\doc\tutorial\src项目

3. 使用SessionFactory.getCurrentSession()需要在hibernate.cfg.xml中如下配置:

   * 如果采用jdbc独立引用程序配置如下:

    <property name="hibernate.current_session_context_class">thread</property>

   * 如果采用了JTA事务配置如下

    <property name="hibernate.current_session_context_class">jta</property>*/

将以上程序中的:

//获得session
session = this.getSession();

修改为:

//获得session
session = this.getHibernateTemplate().getSessionFactory().openSession();

这样异常就解决了。

Hibernate常见异常与分析处理(收集整理)

2010年1月7日 IT北瓜 没有评论

1.    net.sf.hibernate.MappingException

分析处理:

当出现net.sf.hibernate.MappingException: Error reading resource:…异常时一般是因为映射文件出现错误。
当出现net.sf.hibernate.MappingException: Resource: … not found是因为XML配置文件没找到所致,有可能是放置目录不正确,或者没将其加入hibernate.cfg.xml中。

2.    net.sf.hibernate.PropertyNotFoundException

分析处理:

当出现net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class …时,原因一般是因为XML映射文件中的属性与对应的Java类中的属性的getter或setter方法不一致。

3.    org.hibernate.id.IdentifierGenerationException

分析处理:

当出现org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():异常时,一般是因为<id>元素配置不正确,<id>元素缺少其子元素<generator></generator>的配置引起。

<id>元素映射了相应数据库表的主键字段,对其子元素<generator class="">,其中class的取值可以为increment、identity、sequence、hilo、nativ-e……等,更多的可参考hibernate参考文档,一般取其值为native。

4.    a different object with the same identifier value was already associated with the session

分析处理:

当出现a different object with the same identifier value was already associated with the session时,一般是因为在hibernate中同一个session里面有了两个相同标识但是是不同实体。

有如下几种解决方案:
(1)使用session.clean(),如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。
(2)使用session.refresh(object),当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。
(3)session.merge(object),Hibernate里面自带的方法,推荐使用。

5.    SQL Grammer Exception,Could not execute JDBC batch update

分析处理:

当出现SQL Grammer Exception,Could not execute JDBC batch upd-ate异常时,一般是由如下问题引起:
(1)SQL语句中存在语法错误或是传入的数据有误;
(2)数据库的配置不合法,或者说是配置有误。较容易出现的有数据表的映射文件
(,hbm.xml文件)配置有误;Hibernate.cfg.xml文件配置有误;
(3) 当前的数据库用户权限不足,不能操作数据库。以是以Oracle 数据库为例,这种情况下在错误提示中会显示java.sql.BatchUpdateException: ORA-01031: ins-ufficient privileges这样的信息。

6.  Caused by: org.dom4j.DocumentException: Invalid byte 2 of2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of2-byte UTF-8 sequence.

分析处理:

如果出现这行错误说明你的XML配置文件有不规范的字符,检查下。

7.  hibernate/Hello_Bean.hbm.xml

分析处理:

如果出现这行错误说明你的hibernate的XML配置文件有错

8.  net.sf.hibernate.HibernateException: JDBC Driver class not f-ound: org.gjt.mm.mysql.Driver:

分析处理:

没有找到数据库驱动程序

9.  The database return no natively generate indentity value

分析处理:

主键没有添加增量

10.  net.sf.hibernate.PropertyValueException:not-null property ref-erences a null or transient

分析处理:

value:com.pack.Rordercontent.异常产生原因:Rordercontent对象的非空属性Rordertable引用了一个临时对象。

11.  net.sf.hibernate.TransientobjectException:objiect references an unsaved transient instance – save the transient instance before flushing: com.pack.Rordertable

分析处理:

持久化对象的某个属性引用了一个临时对象Rordertable

12.   net.sf.hibernate.PropertyNotFoundException

分析处理:

当出现net.sf.hibernate.PropertyNotFoundException: Could not find a setter for pr-operty name in class …时,原因一般是因为XML映射文件中的属性与对应的Java类中的属性的getXXX或setXXX方法不一致。

13.  tce of my chapter.pack6.Rordertable altered from 1 to 100:

分析处理:

企图修改处于持久化状态的对象的OID。修改了处于持久化对象的OID在Session清理缓存时就会抛出此异常(对象处于持久化状态时。不允许程序随意修改它的OID。注意:无论java对象处于临时状态、持久化状态还是游离状态,应用程序都不应该修改它的OID。因为,比较安全的做法时,在定义持久化类时,吧他的setId()方法设为private类型,禁止外部程序访问该方法)。

14.  net.sf.hibernate.MappingException: Unknown entity class:未知的实体类

分析处理:

Hibernate把持久化类的属性分为2种:值(Value)类型和实体(Entity)类型。值类型和实体类型最重要的区别是前者没有OID,不能被单独持久化,它的声明周期依赖于所属的持久化类的对象的声明周期,组件类型就是一种值类型;而实体类型有OID,可以被单独持久化。

15.   net.sf.hibernate.QueryException: undefined alias

分析处理:

我猜想出项这种错误的原因有很多种:可能是大小写问题,还有其他很多种可能

16.   net.sf.hibernate.NonUniqueResultException:

分析处理:

检索单个对象时,查询结果包含多个对象,但没有调用setMaxResult(1)方法

17.  net.sf.hibernate.QueryException: Not all named paramete-rrs have been set

分析处理:

使用setProperties()方法:用于把命名参数与一个对象的属性值绑定时,对象中没有匹配的名字相同的属性。

Hibernate3.3.2+Spring2.5.6+Struts2.1.6整合异常解决——不断更新

2009年9月24日 IT北瓜 5 条评论

1、org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ’sessionFactory’ is defined

异常说明:sessionFactory bean未定义

异常原因:applicationContext.xml配置文件中使用其他名字sessionFactory

<property name="sessionFactory">
    <ref bean="dmccSessionFactory"/>
</property>

上面的代码中bean=”dmccSessionFactory”,这是造成异常的原因。这里用了别名,而不是默认的bean=”sessionFactory”。这种情况下在web.xml文件中配置OpenSessionInViewFilter则需要对使用的别名进行声明。

<filter>
    <description>处理Hibernate的懒加载问题</description>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <description>默认情况下,这个Filter会在Spring的bean池中找一个叫做sessionFactory的bean。如果使用了其他名字的SessionFactory,则应该在这里 指定这个名字。</description>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>dmccSessionFactory</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

2、

ERROR – failed to lazily initialize a collection of role: leeo.pojo.TPermission.roles, no session or session was closed

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: leeo.pojo.TPermission.roles, no session or session was closed

异常说明:session已关闭,无法初始化懒加载数据集role

异常原因:Hibernate映射文件中配置了lazy="true",同时也启用了OpenSessionInViewFilter。当实现ajax检测role名称是否可用时报以上异常,我这里检测role名称是利用json格式返回数据的。json返回数据的特点是:如果action中的属性有get()方法并且该属性没有transient修饰,那么json就会将其返回。在我的TRoleAction中有private TRole role;并且role有get()方法,所以在检测名称是否可用后返回的json数据中会将这一属性返回,但是检测名称的可用性根本不需要用到这个属性,异常就是由于启用懒加载机制下json中返回role这个属性需要查询数据库,而这时session已经关闭,因此报异常。

解决方法:用transient对role属性进行修饰,即:private transient TRole role;这样json就不会将其返回,因为也不用再次查询关联数据不会出现session已关闭的异常。