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?
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.
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?
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".