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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:16:36+00:00 2026-05-20T08:16:36+00:00

I am using JPA (with hibernate as the provider) in spring for a web-app.

  • 0

I am using JPA (with hibernate as the provider) in spring for a web-app.
I have a few methods that construct a complex object to be passed to the UI. As part of theses methods flow, a few select SQL statements are executed. The problem is that the entity manager flushes before each select, this action takes a lot of time and is hindering the performance. Is there a way to prevent the entity manager from flushing before each select? (I don’t care for stale data in the select selects above)

Thank you.

Here is my GenericDAO

@Repository
public abstract class GenericDAOWithJPA<T, ID extends Serializable> implements IGenericDAO<T, ID> {
    private static final int MAX_RETRIES = 3;
    private static final long WAIT_INTERVAL_MS = 1000;
    static final Logger LOG = LoggerFactory.getLogger(GenericDAOWithJPA.class);

    private Class<T> persistentClass;

    protected EntityManager entityManager;

    @SuppressWarnings("unchecked")
    public GenericDAOWithJPA() {
            this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }


    @Override
    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
        this.entityManager.setFlushMode(FlushModeType.COMMIT)
    }


    @Override
    public Class<T> getPersistentClass() {
        return persistentClass;
    }

    /* (non-Javadoc)
     * @see com.legolas.dao.IGenericDAO#find(ID)
     */
    @Override
    public T find(ID id) {
        return entityManager.find(persistentClass, id);
    }


    @Override
    public T getReference(ID id) {
        return entityManager.getReference(persistentClass, id);
    }

    /* (non-Javadoc)
     * @see com.legolas.dao.IGenericDAO#persist(T)
     */
    @Override
    public void persist(T entity) {
        entityManager.persist(entity);
    }

    @Override
    public void persist(List<T> entityList) {
        for (T entity : entityList) {
            persist(entity);
        }
    }



    @Override
    public T merge(T entity) {
        return entityManager.merge(entity);
    }


    @Override
    public void remove(T entity) {
        entityManager.remove(entity);
    }


    @Override
    @SuppressWarnings("unchecked")
    public List<T> findAll() {
        return entityManager.createQuery("Select t from " + persistentClass.getSimpleName() + " t").getResultList();
    }


    @Override
    @SuppressWarnings("unchecked")
    public List<T> findInRange(int firstResult, int maxResults) {
        return entityManager.createQuery("Select t from " + persistentClass.getSimpleName() + " t").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }


    @Override
    public long count() {
        return (Long) entityManager.createQuery("Select count(t) from " + persistentClass.getSimpleName() + " t").getSingleResult();
    }

    @Override
    public void flush() {
        this.entityManager.flush();
    }

    @Override
    public void refresh(T entity) {
        int retry = 0;
        RuntimeException lastException = null;
        do {
        try {
            this.entityManager.refresh(entity);
        } catch (OptimisticLockException e) {
            retry++;
            lastException = e;
            LOG.debug("OptimisticLockException retry {}", retry);
            try {
                Thread.sleep(WAIT_INTERVAL_MS);
            } catch (InterruptedException e1) {
                retry = MAX_RETRIES;
            }
        }
        } while (lastException != null && retry < MAX_RETRIES);

    }
}
  • 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-20T08:16:37+00:00Added an answer on May 20, 2026 at 8:16 am

    The following code disables flush before queries (flush would occur only before commit):

    em.setFlushMode(FlushModeType.COMMIT);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Spring with Hibernate as a JPA provider and are trying to get
I'm using Spring + JPA + Hibernate. I'm trying to enable Hibernate's second level
I'm using Hibernate Validator 4.0.2, Spring 3.0 and Hibernate 3.3.2 (which, as I understand
I'm using Hibernate for a Java-based Web Application and want to add full-text search
A project I am working on uses Spring 2.5 & JPA with Hibernate as
I've been trying to use polymorphic query with JPA (hibernate as provider) in the
Is using the 'synchronized' keyword on methods in a Java DAO going to cause
today I have coded a test case for my application, to see how transactions
This question is, at it's core, a design question. I'll use a Java/Java EE
I'm new to Guice, and I'm working it in to an application with a

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.