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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:13:55+00:00 2026-05-22T01:13:55+00:00

I am using Spring transactions so the transaction is still active when POJO to

  • 0

I am using Spring transactions so the transaction is still active when POJO to DTO conversion occurs.

I would like to prevent Dozer from triggering lazy loading, so that hidden sql queries never occur : all fetching has to be done explicitly via HQL (to get the best control on performances).

  1. Is it a good practice (I can’t find it documented anywhere) ?

  2. How to do it safely ?

I tried this before DTO conversion :

PlatformTransactionManager tm = (PlatformTransactionManager) SingletonFactoryProvider.getSingletonFactory().getSingleton("transactionManager");
tm.commit(tm.getTransaction(new DefaultTransactionDefinition()));

I don’t know what happens to the transaction, but the Hibernate session doesn’t get closed, and the lazy loading still occurs.

I tried this :

SessionFactory sf = (SessionFactory) SingletonFactoryProvider.getSingletonFactory().getSingleton("sessionFactory");
sf.getCurrentSession().clear();
sf.getCurrentSession().close();

And it prevents lazy loading, but is it a good practice to manipulate session directly in the application layer (which is called “facade” in my project) ? Which negative side effects should I fear ? (I’ve already seen that tests involving POJO -> DTO conversions could no more be launched through AbstractTransactionnalDatasource Spring test classes, because this classes try to trigger a rollback on a transaction which is no more linked to an active session).

I’ve also tried to set propagation to NOT_SUPPORTED or REQUIRES_NEW, but it reuse the current Hibernate session, and doesn’t prevent lazy loading.

  • 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-22T01:13:55+00:00Added an answer on May 22, 2026 at 1:13 am

    The only generic solution I have found for managing this (after looking into Custom Converters, Event Listeners & Proxy Resolvers) is by implementing a Custom Field Mapper. I found this functionality tucked away in the Dozer API (I don’t believe it is documented in the User Guide).

    A simple example is as follows;

    public class MyCustomFieldMapper implements CustomFieldMapper 
    {
        public boolean mapField(Object source, Object destination, Object sourceFieldValue, ClassMap classMap, FieldMap fieldMapping) 
        {       
            // Check if field is a Hibernate collection proxy
            if (!(sourceFieldValue instanceof AbstractPersistentCollection)) {
                // Allow dozer to map as normal
                return false;
            }
    
            // Check if field is already initialized
            if (((AbstractPersistentCollection) sourceFieldValue).wasInitialized()) {
                // Allow dozer to map as normal
                return false;
            }
    
            // Set destination to null, and tell dozer that the field is mapped
            destination = null;
            return true;
        }   
    }
    

    This will return any non-initialized PersistentSet objects as null. I do this so that when they are passed to the client I can differentiate between a NULL (non-loaded) collection and an empty collection. This allows me to define generic behaviour in the client to either use the pre-loaded set, or make another service call to retrieve the set (if required). Additionally, if you decide to eagerly load any collections within the service layer then they will be mapped as usual.

    I inject the custom field mapper using spring:

    <bean id="dozerMapper" class="org.dozer.DozerBeanMapper" lazy-init="false">
        <property name="mappingFiles">
            ...
        </property>
        <property name="customFieldMapper" ref="dozerCustomFieldMapper" />
    </bean>
    <bean id="dozerCustomFieldMapper" class="my.project.MyCustomFieldMapper" />
    

    I hope this helps anyone searching for a solution for this, as I failed to find any examples when searching the Internet.

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

Sidebar

Related Questions

I'm using Spring's support for JDBC. I'd like to use JdbcTemplate (or SimpleJdbcTemplate) to
I am using Spring's declarative transactions (the @Transactional annotation) in aspectj mode. It works
I´m using Transaction Binding=Explicit Unbind in the connection string as recommended here since I´m
I'm using Spring.net with NHiberante (HibernateTemplate) to implement my DAO's. I also have some
I am using Spring Forms for my web application. For nested properties, the form
I've been using spring for some time, but I always wondered how does it
We're using Spring 2.5.4/Hibernate 3.2/Websphere Application Server 6.1.0.17. We've deployed the application on an
We're using Spring/Hibernate on a Websphere Application Server for AIX. On my Windows machine,
I`m developing an application using Spring WebFlow 2, Facelets and JSF. One of my
We have started using Spring framework in my project. After becoming acquainted with the

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.