简体   繁体   English

JPA Hibernate 查询与本机查询

[英]JPA Hibernate query vs Native query

I use Spring Data JPA (hibernate) generated queries for fetching data from my Sqlserver .我使用Spring Data JPA (hibernate)生成的查询从我的Sqlserver获取数据。 Now i am getting performance related issues in my system.现在我的系统出现了与性能相关的问题。

Load findByLoadId(Integer loadId);加载 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.此查询返回 25 个单元格数据,但我只使用其中的 5 个数据。

can i use direct native query like我可以使用直接本机查询吗

select id,date,createdBy,createdOn,loadName from Load where loadId=:loadId选择 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 ?但是,如果本机查询是可建议的,那么我会遇到诸如ORM 框架是否会通过从数据库获取不需要的数据来降低性能的问题?

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.您的问题的答案是肯定的,ORM 框架可能倾向于在后台执行SELECT * ,这可能会导致不需要的信息通过网络发送到您的应用程序。 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.如果 JPA 存储库接口以这种方式运行,您可以切换到显式 JPA 查询(例如使用@Query注释),甚至是本机查询。 Then, just select the columns you want.然后,只需选择所需的列。 The issue here is that ORM frameworks map object templates (eg classes) to entire database tables.这里的问题是 ORM 框架将对象模板(例如类)映射到整个数据库表。 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.如果您选择仅选择某些列,您可能需要在 Java 端做一些杂耍。 Note that if the use a JPA query, your code would still, in theory, be database independent.请注意,如果使用 JPA 查询,理论上您的代码仍将独立于数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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