简体   繁体   中英

JPA Hibernate query vs Native query

I use Spring Data JPA (hibernate) generated queries for fetching data from my Sqlserver . Now i am getting performance related issues in my system.

Load findByLoadId(Integer loadId);

This is the query i am using to get data. This query returns 25 cell data but i only use 5 data from that.

can i use direct native query like

select id,date,createdBy,createdOn,loadName from Load where loadId=:loadId

but if native query is suggestable then I am having question like Does ORM frameWork reduce performence by getting unneeded data from Database ?

By "data cell" I assume that you are referring to database table columns, and not to records. The answer to your question is that yes, ORM frameworks might tend to just do a SELECT * under the hood, which can result in unwanted information being sent across the network to your application. If the JPA repository interface is behaving this way, you may switch to either an explicit JPA query (eg using the @Query annotation), or even a native query. Then, just select the columns you want. The issue here is that ORM frameworks map object templates (eg classes) to entire database tables. So, the concept of entity implicitly includes every database column. If you go with the option of selecting only certain columns, you may need to do some juggling on the Java side. Note that if the use a JPA query, your code would still, in theory, be database independent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM