Spring + Hibernate Lazy Load Error

I have a strange situation where I get a lazy boot problem on one of my controllers. Note. I use OpenSessionInViewInterceptor and annotate my "service" level as Transactional.

I have several different ways to load a Person object, one by its key, and one of its SSN. On my person object, I have a set of roles that I lazily load. When I load with the key, I can access the list as expected. When I load an SSN, I cannot access the list.

In my service level configuration file, I added:

<tx:annotation-driven />

Here is my piece of the service level that loads a person using Key and SSN (I know that this should be in a DAO / reorganized - this is legacy code). Note: none of the versions in the SSN part allows loading - both are in the same class:

@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;
}

The only difference in my controllers is one key download, and one SSN. Here is the relevant part of stacktrace:

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)

Odd note: if I load a person by key immediately after loading the SSN, I can read the collection without any problems.

Edit:

Here are the logs before the stack trace:

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!
+5
source share
1 answer

, , , , . , . , OpenSessionInViewInterceptor, , , . . , "". , . . , , . webapp OpenSessionInViewFilter .

+2

All Articles