You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by cleverpig <gr...@gmail.com> on 2009/09/23 12:10:29 UTC

T5:why I got this exception-"hibernate.LazyInitializationException"?

I wrote a simple query in ListView page,there is a select component in
the page bottom,which will load category list from DB by Hibernate.
the ListView.tml will provider query parameter(selectCat) to the ListView class.

when i running them,i got this exception:

2009-09-23 17:45:41 http-8080-2 DEBUG scrip.ListView:[161]:getSelectCat...
2009-09-23 17:45:41 http-8080-2 DEBUG scrip.ListView:[151]:getCatEncoder...
2009-09-23 17:45:41 http-8080-2 ERROR
hibernate.LazyInitializationException:[19]:could not initialize proxy
- no Session
org.hibernate.LazyInitializationException: could not initialize proxy
- no Session
	at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
	at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
	at com.packtpub.t5first.entities.Category$$EnhancerByCGLIB$$13d50a18.getId(<generated>)
	at com.packtpub.t5first.utils.CategoryEncoder.toClient(CategoryEncoder.java:20)
	at com.packtpub.t5first.utils.CategoryEncoder.toClient(CategoryEncoder.java:1)
	at org.apache.tapestry5.corelib.components.Select.options(Select.java:224)

Here is my code:

---my ListView page class---
public class ListView{
...
        @InjectService("catService")
        private CategoryService catService;

        public SelectModel getCatSelectModel(){
		log.debug("getCatSelectModel...");
		return new CategorySelectModel(catService.list());
	}
	
	public ValueEncoder<Category> getCatEncoder(){
		log.debug("getCatEncoder...");
		return new CategoryEncoder(catService);
	}
	@Persist
	private Category selectCat;
	
	@Inject
	private CategoryDAO catDao;
	
	public Category getSelectCat() {
		log.debug("getSelectCat...");
		return selectCat;
	}

	public void setSelectCat(Category selectCat) {
		log.debug("setSelectCat...");
		this.selectCat = selectCat;
	}
...
}

---my CategoryEncoder class---
public class CategoryEncoder implements ValueEncoder<Category>{
	private CategoryService catService;
	
	public CategoryEncoder(CategoryService catService) {
		super();
		this.catService = catService;
	}

	@Override
	public String toClient(Category cat) {
		return String.valueOf(cat.getId());
	}

	@Override
	public Category toValue(String id) {
		return catService.getFromId(Integer.parseInt(id));
	}
}

---ListView.tml---
<html t:type="layout"
	title="${message:page-scrip-listview}"
	xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
	<t:form t:id="scripListQueryForm">
		<div id="dataListScope">
			<t:grid
				t:id="scripGrid"
				t:source="listDS"
				t:empty="There are currently no search results"
				model="beanModel"
				rowsPerPage="5"
		    	row="current"
		    	exclude="content"
		    	reorder="rownumber,id,title,releaseDate"
		    	pagerPosition="bottom"
		    	style="width:620px;">
		    </t:grid>
	    </div>
	    <div id="operationScope">
	    	<p>
	    		<t:label for="queryCat"/>
	    		<t:select t:id="queryCat"
	    			t:model="catSelectModel"
	    			t:value="selectCat"
	    			t:encoder="catEncoder"/>
	    		<t:Submit t:id="submitQueryButton"/>
	    	</p>
	    </div>
    </t:form>
</html>

who can give me some suggestion? Any help is welcome! :)
-- 
cleverpig(Dan)
Location: Beijing
Address: Room 4018,No.A2 South Avenue Fuxingmen Beijing,P.R.China
Zipcode: 100031
MSN: great_liudan@hotmail.com
QQ: 149291732
Skype: cleverpigatmatrix
Facebook ID:cleverpig
Blog: www.cleverpig.name
Tags: del.icio.us/cleverpig
Twitter: twitter.com/cleverpig
新浪微博: t.sina.com.cn/cleverpig
Organization: www.beijing-open-party.org
Organ@Facebook: http://www.facebook.com/group.php?gid=8159558294

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5:why I got this exception-"hibernate.LazyInitializationException"?

Posted by cleverpig <gr...@gmail.com>.
Ho! I found the answer,it's just not a hibernate issue,just since it
was a wrong way to write ValueEncoder implementation.

At first.it's my old CategoryEncoder class:
public class CategoryEncoder implements ValueEncoder<Category>{
       private CategoryService catService;

       public CategoryEncoder(CategoryService catService) {
               super();
               this.catService = catService;
       }

       @Override
       public String toClient(Category cat) {
               return String.valueOf(cat.getId());
       }

       @Override
       public Category toValue(String id) {
               return catService.getFromId(Integer.parseInt(id));
       }
}

these code would get client or server side value from catService,this
is the mistake.

I fixed to these,at first get the category object List,and cut off the
relation between ValueEncoder and catService:

public class GenericValueEncoder<T> implements ValueEncoder<T> {

       protected List<T> list;
       protected final PropertyAccess access;
       protected final String fieldName;

       public GenericValueEncoder(List<T> list, String fieldName,
PropertyAccess propertyAccess) {
               this.list = list;
               this.fieldName = fieldName;
               this.access = propertyAccess;
       }

       public String toClient(T obj) {
               if (fieldName == null) {
                       return obj + "";
               } else {
                       return access.get(obj,fieldName)+"";
               }
       }

       public T toValue(String string) {
               for (T obj : list) {
                       if (toClient(obj).equals(string)) return obj;
               }
               return null;
       }
}

public class CategoryValueEncoder extends GenericValueEncoder<Category>{
	
	public CategoryValueEncoder(List<Category> cats,PropertyAccess
propertyAccess) {
		super(cats,"id",propertyAccess);
	}
}

in ListView class,will be fixed as:
...
@Inject
private PropertyAccess propAccess;
public ValueEncoder<Category> getCatEncoder(){
	log.debug("getCatEncoder...");
	return new CategoryValueEncoder(catService.list(),propAccess);
}
...

ok,it's my resolution.
its knowledge came from tapestry
wiki:http://wiki.apache.org/tapestry/Tapestry5SelectObject?highlight=%28ValueEncoderSource%29
thanks,tapestry and friends!

On Wed, Sep 23, 2009 at 7:22 PM, Thiago H. de Paula Figueiredo
<th...@gmail.com> wrote:
> Em Wed, 23 Sep 2009 07:10:29 -0300, cleverpig <gr...@gmail.com>
> escreveu:
>
>> org.hibernate.LazyInitializationException: could not initialize proxy
>
> This is a Hibernate issue, not a Tapestry one. By the way, understanding
> this exception is basic Hibernate knowledge. Your solution is in the manual.
> ;)
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java consultant, developer, and instructor
> http://www.arsmachina.com.br/thiago
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
cleverpig(Dan)
Location: Beijing
Address: Room 4018,No.A2 South Avenue Fuxingmen Beijing,P.R.China
Zipcode: 100031
MSN: great_liudan@hotmail.com
QQ: 149291732
Skype: cleverpigatmatrix
Facebook ID:cleverpig
Blog: www.cleverpig.name
Tags: del.icio.us/cleverpig
Twitter: twitter.com/cleverpig
新浪微博: t.sina.com.cn/cleverpig
Organization: www.beijing-open-party.org
Organ@Facebook: http://www.facebook.com/group.php?gid=8159558294

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5:why I got this exception-"hibernate.LazyInitializationException"?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Em Wed, 23 Sep 2009 07:10:29 -0300, cleverpig <gr...@gmail.com>  
escreveu:

> org.hibernate.LazyInitializationException: could not initialize proxy

This is a Hibernate issue, not a Tapestry one. By the way, understanding  
this exception is basic Hibernate knowledge. Your solution is in the  
manual. ;)

-- 
Thiago H. de Paula Figueiredo
Independent Java consultant, developer, and instructor
http://www.arsmachina.com.br/thiago

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org