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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:31:23+00:00 2026-06-18T11:31:23+00:00

Im using JPA in my Java ee application. I have a table A mapped

  • 0

Im using JPA in my Java ee application. I have a table A mapped to a class A. Now when I query the entity (e.g. by its id) it always returns a different object. Example:

public A getAById(int id) {
    A obj = (A) em.createNamedQuery("getAById")
            .setParameter("id", id).getSingleResult();
    return obj;
}

public void test(){
    getAById(1)==getAById(1) //is false
}

Is there a way to tell JPA not to always create new instances when querying from a database but to return an existing object if it hast alreay been queried?

//EDIT
This article helped me a lot:
http://en.wikibooks.org/wiki/Java_Persistence/Caching#Example_JPA_2.0_Cacheable_annotation

  • 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-06-18T11:31:25+00:00Added an answer on June 18, 2026 at 11:31 am

    JavaEE containers wrap the JPA providers EntityManager in a proxy, so the behavior you see is likely because your container gets a new EM outside of a transaction. Your two getAById(1) calls are going to two different EntityManagers underneath the covers. You can get around this by wrapping them in a single transaction, forcing the container to use the same entity manager for the life of the transaction.

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

Sidebar

Related Questions

My application uses JPA to access the backend database. I have a Java class
I have an existing google app engine java application runs with jpa and now
I have created a simple java web (Vaadin) application project using JPA and it
I have a fairly standard Java EE6 web application using JPA 2 with dependency
I'm using Jpa with GAE. I have an Entity with collection of sub Entity
I'm have a lot of time using hibernate (JPA) in java, but i recently
I have a Java web application leveraging JPA. The database instance is specified in
I'm using EclipseLink in Glassfish with my JavaEE application and have some java.util.Locale-columns in
I'm using eclipse juno IDE I have Java application which have src folder. within
I'm using Eclipse Juno IDE and phpMyAdmin. I have java application, for this application

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.