Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Previous replicated result in getResultList()

 
Michael Goossens
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I execute this following code two times once with pk = 4754 and once with pk = 4763. The variable result however will both times contain the exact same object, the one with pk 4754. How is this possible? Does JPA keep the previous result in its cache or something?

 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to be using a ResultSetMapping, most likely an issue with it. Please include it.

Try executing the query without the ResultSetMapping.
 
Michael Goossens
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the result mapping

 
Michael Goossens
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something strange to note. The date in startDate is the same for both requests. And when I do -1 second on the date(for both requests) the result is correct.
 
Michael Goossens
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I think I have it. The primary key of the result is actually not unique. When we do -1 second on the date the keys returned in the two calls will be different. When I do not do the -1 second the keys will be the same. So I think JPA will say: "oh the same key" so we do not retrieve the second object, we just return the one in cache.

Is there a way to force a retrieve of the attributes on a getResultList() even if the primary key is the same as one of the objects in memory?
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not use a column as your Id that is not unique.
Instead of getting the entities, you could just get the data with your native query, and build the objects with the data if you need to.

If you are using EclipseLink, you can bypass the persistence context using the query hint, "eclipselink.maintain-cache"="false".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic