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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:39:11+00:00 2026-05-27T03:39:11+00:00

I am doing some CRUD operations using JPA. For updating an object which is

  • 0

I am doing some CRUD operations using JPA. For updating an object which is the right way to do?

Through update query or through the find method of EntityManager?
I have one Employee object I need to update. Which is the right way?

Please guide 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-27T03:39:12+00:00Added an answer on May 27, 2026 at 3:39 am

    Using executeUpdate() on the Query API is faster because it bypasses the persistent context .However , by-passing persistent context would cause the state of instance in the memory and the actual values of that record in the DB are not synchronized.

    Consider the following example :

     Employee employee= (Employee)entityManager.find(Employee.class , 1);
     entityManager
         .createQuery("update Employee set name = \'xxxx\' where id=1")
         .executeUpdate();
    

    After flushing, the name in the DB is updated to the new value but the employee instance in the memory still keeps the original value .You have to call entityManager.refresh(employee) to reload the updated name from the DB to the employee instance.It sounds strange if your codes still have to manipulate the employee instance after flushing but you forget to refresh() the employee instance as the employee instance still contains the original values.

    Normally , executeUpdate() is used in the bulk update process as it is faster due to bypassing the persistent context

    The right way to update an entity is that you just set the properties you want to updated through the setters and let the JPA to generate the update SQL for you during flushing instead of writing it manually.

       Employee employee= (Employee)entityManager.find(Employee.class ,1);
       employee.setName("Updated Name");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been doing some playing call my service which is on a different domain using
I've created a plugin doing some crud operations. In the web-app/images/icons folder I've got
While doing some refactoring I've found that I'm quite often using a pair or
in some frameworks (symfony) you can generate an admin panel for CRUD operations. i
I know REST is meant to be resource-oriented, which roughly translates to CRUD operations
Doing some javascript self-learning and the program itself is very simple. It searches through
Im doing some data cleansing on some messy data which is being imported into
Doing some client-side JavaScript development, which requires me to call a client-side API which
Doing some debugging in windbg, and I'd like to be able to go through
Doing some jquery animation. I have certain divs set up with an attribute of

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.