2013年8月2日星期五

Resolve Hibernate: could not initialize proxy

 

This can be seen literally: Can not initialize, no session. Also said that the main reason is because the session closed.

 

; in Hibernate, the lazy default proxy. Such hibernate query the data in the database will not put current events associated

 

check out an object, but Save a gain that is worth method: getXxxx (). When we need to use this value, which is used getXxx () method

 

tune when used, Hibernate will use this method from the database and retrieve the corresponding data. But very Fortunately, our session already closed. This

 

because Hibernate's lazy loading strategy In Hibernate sessionFactory is used to manage the session, we once for each database operation

 

will create < span class = "Apple-style-span" style = "font-size: 18px;"> a session object, when the operation is completed, we, hibernate will be in the dao layer immediately close the session. doing so can strictly control

 

session, to avoid low-level errors.

 

 

for this error will generally have three ways to solve.

 

1 , the lazy set to false. This is the easiest way, personally think is more stupid way. because it is in use efficiency as consideration.

 

2 , Use OpenSessionInViewFilter. This method is the session to the servlet filter to manage, whenever a request and then will open

 

start a session, only when the response until after the end off closed. As follows:

 
  
1         <filter-name>hibernateFilter</filter-name>  
2 <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class>
3 </filter
4 <filter-mapping>
5 <filter-name>hibernateFilter</filter-name>
6 <url-pattern>/*</url-pattern>
7 </filter-mapping>
 
 

 

 

The above configuration file in the web.xml configuration.

 

< / p>  

3 , will hibernate grabbed the strategy to join. That is, is left join fetch or inner join fetch syntax. That is in with

 

setting lazy =" false " fetch = "join" can be. Such as:

 
  
1     <many-to-one name="worker" lazy="false" fetch="join" class="com.paixie.domain.Worker"> 
2
3 <column name="positionId"></column>
4
5 </many-to-one>
 
 

 

 

没有评论:

发表评论