我遇到了一个奇怪的情况,在我的控制器中出现了懒加载问题。注意:我正在使用OpenSessionInViewInterceptor,并在我的“服务”层上注释了Transactional。
我有几种不同的方法来加载Person对象,一种是按其键加载,另一种是按其SSN加载。在我的person对象上,我有一个角色集合,需要懒加载。当我按键加载时,我能够像预期的那样访问列表。但是当我按SSN加载时,我无法访问该列表。
在我的服务层配置文件中,我已经添加了:
我的控制器唯一的区别是一个按Key加载,一个按SSN加载。以下是堆栈跟踪的相关部分:
在我的服务层配置文件中,我已经添加了:
<tx:annotation-driven />
这是我的服务层代码,它通过关键字和社保号码加载人员信息(我知道这应该在DAO/重构中完成——这是继承的代码)——请注意,SSN部分中的任何版本都不允许加载——两个版本都在同一个类中:
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public Person loadPersonByKey(final Person.Key personKey) {
Assert.notNull(personKey);
return (Person) getHibernateTemplate().get(Person.class, personKey);
}
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public Person findPersonBySsn(final SocialSecurityNumber ssn) {
@SuppressWarnings("unchecked")
//List<Person> results = getHibernateTemplate().findByNamedParam("from core.model.entities.Person as person where ssn = :ssn", "ssn", ssn);
Criteria crit = getSession().createCriteria(Person.class);
crit.add(Restrictions.eq("ssn", ssn));
List<Person> results = crit.list();
int size = results.size();
Person returnPerson = ((size != 0) ? (Person) results.get(0) : null);
return returnPerson;
}
我的控制器唯一的区别是一个按Key加载,一个按SSN加载。以下是堆栈跟踪的相关部分:
SEVERE: Servlet.service() for servlet springmvc threw exception
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: core.model.entities.Person.memberships, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
at org.hibernate.collection.AbstractPersistentCollection.readElementByIndex(AbstractPersistentCollection.java:176)
at org.hibernate.collection.PersistentMap.get(PersistentMap.java:169)
at core.model.entities.Person.getMemberships(Person.java:870)
at core.springmvc.controllers.find.FindController.defaultAction(FindController.java:164)
奇怪的是,如果我在通过社会安全号码加载人员之后立即按键加载,则可以毫无问题地读取集合。
编辑:
以下是堆栈跟踪之前的日志:
2011-08-02 13:29:32,415 [http-8080-1] DEBUG org.springframework.jdbc.datasource.DataSourceUtils CV#905cde28-e60c-4331 P#75004 - Resetting read-only flag of JDBC Connection [Transaction-aware proxy for target Connection [jdbc:oracle:thin:@(description=(address_list=(address=(host=127.0.0.1)(protocol=tcp)(port=11523))(load_balance=yes)(failover=yes))), UserName=USER_NAME, Oracle JDBC driver]]
2011-08-02 13:29:32,415 [http-8080-1] DEBUG org.hibernate.impl.SessionImpl CV#905cde28-e60c-4331 P#75004 - disconnecting session
2011-08-02 13:29:32,415 [http-8080-1] DEBUG org.hibernate.jdbc.ConnectionManager CV#905cde28-e60c-4331 P#75004 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2011-08-02 13:29:32,415 [http-8080-1] DEBUG org.springframework.jdbc.datasource.DataSourceUtils CV#905cde28-e60c-4331 P#75004 - Returning JDBC Connection to DataSource
2011-08-02 13:29:32,415 [http-8080-1] DEBUG org.hibernate.jdbc.ConnectionManager CV#905cde28-e60c-4331 P#75004 - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!