I'm new to JPA and I wrote simple JPA selects for my web service. I was using classic SQL statements before. It was great, I knew how to get solid speed, but I'm working with 3 different databases in 5 different projects now. So I thought that learning JPA was a good way to use one technology instead of having to know 3 SQL languages. So I created some tables, entities and tried to use JPA. But something was wrong in the first moment - speed.
(I'm using MSSQL 2008)
I compared simple native SQL select and JPA select :
em.createNativeQuery("SELECT c.* FROM Table c", MyTable.class);
and
em.createQuery("SELECT c FROM Table c", MyTable.class)
createNativeQuery
was 5x quicker, but why?
So I started SQL profiler included in MSSQL package and debugged what JPA was doing. I have a table with 100 records and what I was seeing surprised me:
createNativeQuery
creates 1 request to db and result was 100 records in querycreateQuery
creates 100 requests and every request returns 1 record. Something like this:1 SELECT Col1, Col2 FROM TABLE WHERE (ID = ?)<br> 2 SELECT Col1, Col2 FROM TABLE WHERE (ID = ?)<br> . . . 100 SELECT Col1, Col2 FROM TABLE WHERE (ID = ?)
I'm using JPA 2 with Eclipse Link. Is this normal behavior for how JPA works? Or have I done something wrong in my settings.