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

  • Home
  • SEARCH
  • 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 6252441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:48:06+00:00 2026-05-24T13:48:06+00:00

I have a SpringMVC+Hibernate web application with the following files: applicationContext.xml <bean id=dataSource destroy-method=close

  • 0

I have a SpringMVC+Hibernate web application with the following files:

applicationContext.xml

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://localhost:5432/db"/>
    <property name="username" value="kjhkjkj"/>
    <property name="password" value="dsfa@efe45"/>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>

<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="txManager"/>
    <property name="transactionAttributes">
        <props>
            <prop key="*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
    <property name="target">
        <bean class="dao.DocumentViewDao">
            <property name="sessionFactory" ref="hibernateSessionFactory"/>
        </bean>
    </property>
    <property name="proxyTargetClass" value="true"/>
</bean>

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
        <list>
    <value>orm/Document.hbm.xml</value>
    <value>orm/UploadedDocument.hbm.xml</value>
    <!-- More resource files go here -->
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
            hibernate.show_sql = true
            hibernate.format_sql = true
            hibernate.generate_statistics = true
            hibernate.cache.use_query_cache = true
            hibernate.cache.use_second_level_cache = true
            hibernate.cache.provider_class = org.hibernate.cache.EhCacheProvider
            hibernate.query.substitutions = true 't', false 'f'
        </value>
    </property>
</bean>

<bean id="documentViewDao" class="dao.DocumentViewDao">
    <property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>

dao.DocumentViewDao

@Transactional
public class DocumentViewDao extends HibernateDaoSupport
{
public List all ( )
        throws HibernateException
{
    Session session = this.getSessionFactory ( ).getCurrentSession ( );

    return session.createQuery ( new StringBuilder ( 35 )
                    .append ( "SELECT d.id AS id, d.identifier AS identifier, d.title AS title, u.version AS version, u.effectFrom AS effectFrom " )
                    .append ( "FROM Document AS d " )
                    .append ( "LEFT OUTER JOIN d.uploadedDocumentsById AS u " )
                    .append ( "WITH u.isCurrent = true" )
                    .toString ( ) )
            .setCacheable ( true )
            .setResultTransformer( Transformers.ALIAS_TO_ENTITY_MAP )
            .list ( );
}

public Document getDocument ( int documentId )
        throws HibernateException
{
    Session session = this.getSessionFactory ( ).getCurrentSession ( );

    Document document = null;

    Query q = session.createQuery ( "FROM IsoDocument AS d " +
                                    "LEFT OUTER JOIN FETCH d.uploadedDocumentsById " +
                                    "WHERE d.id = :documentId"
    );
    q.setParameter ( "documentId", documentId );
    q.setCacheable ( true );
    document = ( Document ) q.uniqueResult ( );

    return document;
}
}

controller.DocumentController

public class DocumentController implements Controller
{   
    public ModelAndView handleRequest ( HttpServletRequest request, HttpServletResponse response )
            throws Exception
    {
        ModelAndView modelAndView = new ModelAndView ( "document" );

        WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext ( );
        DocumentViewDao documentViewDao = ( DocumentViewDao ) context.getBean ( "transactionProxy" );

        Document document = DocumentViewDao.getDocument ( Integer.parseInt ( request.getParameter ( "id" ) ) );
        modelAndView.addObject ( "document", document );

        return modelAndView;
    }
}

The ORM classes and mapping should be OK, I won’t write them (lost of lines :))
So the problem is I get

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: orm.Document.uploadedDocumentsById, no session or session was closed

when the page generated by controller.DocumentController is refreshed. Obviously there is a problem with the caching and transactions which I cannot solve. Moreover, using all() from dao.DocumentViewDao doen’t throw such an exception (but there isn’t FETCH in the query). So do you have any ideas? I also will be happy to any comments and/or suggestions on this code in orger to improve it (am new to Spring and Hibernate).

  • 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-24T13:48:07+00:00Added an answer on May 24, 2026 at 1:48 pm

    When a query returning entity instance(s) is cacheable and you have a cache hit, the cache returns only the ID(s) of the entity(ies), and then the session is used to load the entity(ies). This means that the join fecth is not applied anymore when you have a cache hit. This also means that all the entity instances returned by the query should also be in the second-level cache, else the performance might in fact be worse than without the query cache.

    You should thus use Hibernate.initialize(document.getUploadedDocumentsById() in the DAO to make sure it’s initialized.

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

Sidebar

Related Questions

In SpringMVC in src/main/resources I have my hibernate.cfg.xml I have a class that's my
I have Hibernate Transaction manager configured inside Spring MVC controller. <bean id=dataSource class=org.apache.commons.dbcp.BasicDataSource destroy-method=close>
I have a Spring MVC based Web Application with Hibernate. Following is the directory
I have a SpringMVC web service for uploading files which looks like this: @RequestMapping(value=/upload.json,
I have two class definitions in my Spring MVC web application named Class and
I have java application with web interface (Spring MVC) using Hibernate. I have quite
I basically have the following flow: XML -> JSON -> Spring MVC -> jsp
I am using Spring MVC to build my web application, and I have a
I have the following setup. Spring 3.0.5 Hibernate 3.5.6 MySql 5.1 To save a
So I've got a simple web application using Spring MVC + Hibernate and am

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.