【发布时间】:2011-08-02 14:52:21
【问题描述】:
我遇到了一个奇怪的情况,我的一个控制器出现延迟加载问题。 注意:我正在使用 OpenSessionInViewInterceptor 并将我的“服务”层注释为事务。
我有几种不同的方法来加载 Person 对象,一种是通过它的键,另一种是通过它的 SSN。在我的 person 对象上,我有一组我懒加载的角色。当我通过 Key 加载时,我可以按预期访问列表。当我通过 SSN 加载时,我无法访问该列表。
在我的服务层配置文件中我已经添加了:
<tx:annotation-driven />
这是我的服务层片段,它通过密钥和 SSN 加载人员(我知道这需要在 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)
奇怪的是,如果我在通过 SSN 加载后立即通过密钥加载此人,我可以毫无问题地阅读该集合。
编辑:
以下是堆栈跟踪之前的日志:
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!
【问题讨论】:
-
另外,如果调用 Hibernate.initialize(person) 我仍然会得到初始化异常——即使我在服务层这样做。
标签: java hibernate spring spring-mvc