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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:12:58+00:00 2026-05-27T06:12:58+00:00

Using JPA/Eclipse TopLink. I’m updating a table (using createNativeQuery) as shown below: Query query

  • 0

Using JPA/Eclipse TopLink.

I’m updating a table (using createNativeQuery) as shown below:

Query query = em.createNativeQuery("UPDATE Products SET productName='" + p.getProductName() + "',productVendor='" + p.getProductVendor() + "',productDescription='" + p.getProductDescription() + "',quantityInStock=" + p.getQuantityInStock() + ",buyPrice =" + p.getBuyPrice() + ",msrp=" + p.getMsrp() + " WHERE productCode='" + p.getProductCode() + "'");
query.executeUpdate();

The update is reflected in the DB (MySQL)

For retrieving (using createQuery) as show below:

Query query1 = em.createQuery("SELECT p from Products p where p.productCode='"+searchTerm+"'");
return query1.getResultList();

However, the ResultList returned is always the data before the update. But when I use the createNativeQuery instead of createQuery the latest updated data is returned. What could the possible error be with createQuery method?

  • 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-27T06:12:59+00:00Added an answer on May 27, 2026 at 6:12 am

    when you use batch update or native queries, you’re bypassing the first-level cache. Toplink has no way to know that the entities it already has in its first-level cache have been changed. So the entity manager must be cleared in order for the query to retrurn refreshed objects.

    But the best way is probably to avoid batch updates in the first place. Your first query could be replaced by a JPQL query that loads the product with the given code, and then simply updates the Product entity. Even if you keep the update query, it could be written in JPQL rather than SQL (but you would still have the problem you’re having).

    Finally, your queries should use parameters to avoid injections and make sure everything is properly escaped:

    Query query1 = em.createQuery("SELECT p from Products p where p.productCode = :searchTerm);
    query1.setParameter("searchTerm", searchTerm);
    return query1.getResultList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Spring-Roo project set up using Eclipse link for my JPA persistence
Can someone suggest me how to build up the following query using JPA Criteria
Using JPA query language, how do I determine the size (number of rows) in
I am using EclipseLink as JPA Provider. I want Eclipse Link to Generate my
I'm using this tutorial: http://www.objectdb.com/tutorial/jpa/eclipse/ee/ejb I keep seeing words prefixed with @. Is there
I'm using this tutorial: http://www.objectdb.com/tutorial/jpa/eclipse/ee/ejb Is the persist function required? The class isn't extending
I am developing a web application using JSF and JPA(Eclipse link). I have two
I'm using Eclipse 3.7 with Eclipse Gemini JPA / DBAccess. I implemented a declarative
I'm using EclipseLink 2.3.1 to model self referencing table with JPA 2. I get
I'm using JPA (Hibernate's implementation) to annotate entity classes to persist to a relational

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.