Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 943077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:22:42+00:00 2026-05-15T22:22:42+00:00

A little background: I’m using Spring and Hibernate to create a really simple domain/dao/service

  • 0

A little background: I’m using Spring and Hibernate to create a really simple domain/dao/service structure. I’m also using Autowiring to inject all my beans into their happy places.

While refactoring, I recently got the all-too-popular error message “could not initialize proxy – no Session” when trying to access a FetchType.LAZY property on my hibernate object. It goes something like this:

public class Person {

...

 @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
 @JoinColumn(name = "pet_id", nullable = false)
 public Pet getPet() {
  return pet;
 }
...
}

I used to have a PersonService bean that accessed the Pet property, and had no trouble doing so. However, I recently refactored the code so that instead of the PersonService looking at the Pet, a PersonHelper looks at it. While my helper bean can see the PersonDao, can make a call to get the person, it cannot access the Pet as my session is closed.

So, I think that I’m unclear as to when I loose my hibernate session. All the configs look fine, and the DAO is getting injected into my helper, just like it used to get injected into my service. I’m not sure why my service could get the Pet just fine, but my helper cannot.

Any help with understand this “SessionFactory mystery” is well appreciated. I realize this might be a complicated subject, so links to some good reading material would rock.

I’ve since changed the code to FetchType.EAGER (working fine), but this riddle is burning a whole in my brain :).

Per requests, here is a (simplified) look into my config:

    <bean id="personSvc" class="org.comp.service.impl.PersonServiceImpl" />
    <bean id="personHelper" class="org.comp.service.helper.PersonHelper" />

    <bean id="personDao" class="org.comp.dao.hibernate.HibPersonDaoImpl">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

...

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="packagesToScan" value="org.comp.domain"/>
        <property name="schemaUpdate" value="true" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">false</prop>

                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
                </prop>
                <prop key="hibernate.cache.provider_configuration_file_resource_path">/hibernate-ehcache.xml</prop>
            </props>
        </property>
    </bean>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>

DAO is autowired into the helper:

@Autowired
private PersonDao personDao;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:22:43+00:00Added an answer on May 15, 2026 at 10:22 pm

    Without seeing the code/context config for PersonService, I can only guess as to why it worked before the refactoring. If you’re using a HibernateInterceptor to wrap session management around your DAO methods, then the session is closed immediately after the method has finished, unless it was opened beforehand (such as by a OpenSessionInViewFilter).

    My guess is that the scope of the HibernateInterceptor may have changed during refactoring so that the session is now closed immediately after the data is fetched. You may want to look at extending the scope of HibernateInterceptor to cover your service/business methods, so that the session is kept around long enough for lazy fetching to work, or, alternatively, use the OpenSessionInViewFilter, which ensures the session is always available.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a little background: I am using Access 2010 to create forms and VBA
a little background here.I've been working with spring+hibernate(JPA+Session)+maven for while now.I'm used to Hibernate
First a little background, I'm using jdk 1.6. I've got a 2 column table
Just a little background on my project: I'm implementing an SMS encryption program using
A little background. I am using plupload to upload files within my application. The
Here's a little background on my solution: ASP.Net MVC app Using Linq-to-SQL with table-per-hierarchy
Before I begin my question a little background info. I have the WCF service
Little background.. I'm in the process of making an OpenGL game using Java and
For a little background: I have a DLL project with the following structure: Rivworks.Model
A little background info - I'm trying to host a RESTful WCF service on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.