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 6207911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:42:56+00:00 2026-05-24T05:42:56+00:00

I have a hibernate DAO that is throwing a failed to lazily initialize a

  • 0

I have a hibernate DAO that is throwing a “failed to lazily initialize a collection of role” Exception when trying to access a member of the returned object that is a bag/Collection.

I understand the scope of the problem throwing the exception. Hibernate returns my object, and for any Collections, returns proxy objects. In my caller, when I go to access those proxy objects, because the hibernate session has expired, this exception is thrown.

What I want to know is, how can I keep the session from expiring using annotations? Is it possible?

For instance if my calling method is:

@RequestMapping("refresh.url")
public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception {
    TestObject myObj = testObjDao.get(id);
    // throws the exception
    myObj.getCollection();

How would I prevent this exception using annotations? I know one solution would be to extend the hibernate session via a callback which in pseudocode might look something like:

@RequestMapping("refresh.url")
public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception {
    session = get hibernate session...
    session.doInSession(new SessionCallback() {
        TestObject myObj = testObjDao.get(id);
        // no longer throws the exception
        myObj.getCollection();
    });

but this seems rather repetitive to have in all of my functions that need to access collections. isn’t there a way to simply slap an @Transactional annotation on there and be done with it? as in:

@RequestMapping("refresh.url")
@Transactional  // this doesn't extend the session to this method?
public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception {
    TestObject myObj = testObjDao.get(id);
    // throws the exception
    myObj.getCollection();

thanks for your help in explaining this to me.

  • 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-24T05:42:57+00:00Added an answer on May 24, 2026 at 5:42 am

    The solution I used was to use the @LazyCollection annotation on the collection in the DAO.

    @OneToMany(mappedBy = "<something here>", cascade = CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a grails project that is throwing the following exception: org.springframework.dao.DataIntegrityViolationException: could not
Which Spring exception maps onto org.hibernate.StaleObjectStateException ? We have a DAO that is annotated
I have a parameterized hibernate dao that performs basic crud operations, and when parameterized
Currently I have one dao, that uses generic methods and hibernate criteria to perform
I have a hibernate call in my DAO that looks like this List<Associate> associate
I have a DAO class which I'm using to try select/update/insert with hibernate and
I have one hibernate sequence, that generates all sequence-numbers in my app. When I
We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager
I have a JPA/Spring application that uses Hibernate as the JPA provider. In one
I have DAO's for each table, that all implement an interface. Now I want

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.