2

I am new to OpenJPA, and I am wondering if calling a native query will bypass the first level cache and go straight to the database.

user1148956
  • 303
  • 1
  • 3
  • 9

1 Answers1

2

A Native query can be for any table in the datastore ... whether Entity-based or not. The L1 cache, in general, has nothing to do with this process, except when your result class for your query is an Entity.

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
  • So if I do a native query where my result is an Entity, are you saying that as far as the L1 cache is concerned, it will work the same as if I did a similar query using JPQL or CriteriaBuilder? – user1148956 Mar 03 '12 at 14:09
  • 2
    The EntityManager of a (compliant) JPA implementation should only return a single Entity for a particular id (i.e only one managed by that EM at any time), so if the query returns an Entity with some id, and that id is already cached then it should return the cached object. – DataNucleus Mar 03 '12 at 14:42
  • OK - that makes sense. So it sounds like in the case of a simple select where the return is an Entity, it shouldn't matter whether I use Native SQL, JPQL or CriteriaBuilder. In a case like this is there any benefit to NOT using native SQL? – user1148956 Mar 03 '12 at 15:13
  • I suggest you look into [this](https://stackoverflow.com/a/19545569/16785907) – Mikhail2048 Jan 01 '22 at 09:08