org.hibernate.LazyInitializationException: could not initialize proxy - no Session

今天在写jbpm获取流程变量的时候出现了这个异常:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
原因就是jbpm的底层采用了懒加载的方式,解决这个异常的方法就是在对象的映射文件中去掉默认的懒加载,例如:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="cn.itcast.jbpm.processVariable.Form" table="itcast_form" lazy="false">
		<id name="id" type="long">
			<column name="id"></column>
			<generator class="increment"></generator>
		</id>
		<property name="title" type="string">
			<column name="title"></column>
		</property>
	</class>
</hibernate-mapping>
方法一:lazy默认是true,改成false就不会出现该异常了。

方法二:就是把这个类改成final类型的,这样它就不会产生代理对象了,如下:
package cn.itcast.jbpm.processVariable;

public final class Form {

	private Long id;
	private String title;
	
	public Form(){
		
	}

	public Form(Long id, String title) {
		this.id = id;
		this.title = title;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}


你可能感兴趣的:(Hibernate)