java 为什么我们需要创建原生查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13288416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:08:00  来源:igfitidea点击:

Why do we need to create native query?

javajpaorm

提问by Vardan Gupta

I am working in a project which uses JPA ORM and framework provides two kinds of method to create queries.

我在一个使用 JPA ORM 的项目中工作,框架提供了两种创建查询的方法。

  • entityManager.createQuery(query1);
  • entityManager.createNativeQuery(query2);
  • entityManager.createQuery(query1);
  • entityManager.createNativeQuery(query2);

I understand the kinds of query string is to be passed to use them, but I don't know exactly why do we need to create native query? Probably we don't want to use ORM capabilities there?

我知道要传递查询字符串的种类以使用它们,但我不知道为什么我们需要创建本机查询?可能我们不想在那里使用 ORM 功能?

回答by m3th0dman

You do not need to create a native query unless you want to. JPQL eventually is translated into SQL by the framework but the framework lets you call the native query also. Why would want to do that:

除非您愿意,否则无需创建本机查询。JPQL 最终由框架转换为 SQL,但框架也允许您调用本机查询。为什么要这样做:

  • Low level access, which means that you can optimize and handle the mapping by yourself; with SQL you actually access the database table while with JPQL you access the entity objects;
  • Maybe you do not want to learn JPQL if you already know SQL
  • You already have the queries written in SQL, and do not have resources/time to port them to JPQL
  • 低级访问,这意味着您可以自己优化和处理映射;使用 SQL 您实际访问数据库表,而使用 JPQL 您访问实体对象;
  • 如果你已经知道 SQL,也许你不想学习 JPQL
  • 您已经拥有用 SQL 编写的查询,并且没有资源/时间将它们移植到 JPQL

回答by Rasmus Franke

createQuery uses JPAs own query language, you select from Class names instead of table names. This is not SQL, it is just similar, and is later transformed to real SQL. Mapping to java classes will be done automatically and actual class instances will be returned as result.

createQuery 使用 JPA 自己的查询语言,您可以从类名而不是表名中进行选择。这不是SQL,只是类似,后来转化为真正的SQL。映射到 java 类将自动完成,实际的类实例将作为结果返回。

createNativeQuery uses real SQL, and will not be able to use JPA features. This method is used in general if you need to do something really odd that is not supported by JPA. A list of Object[] will be returned, and mapping to java objects will have to be done manually. In other words, its just like working with a DB before JPA came to, just slightly more convenient since connection handling is done automatically.

createNativeQuery 使用真正的 SQL,将无法使用 JPA 功能。如果您需要做一些 JPA 不支持的非常奇怪的事情,通常会使用此方法。将返回 Object[] 列表,并且必须手动完成映射到 java 对象。换句话说,它就像在 JPA 出现之前使用 DB 一样,只是稍微方便一些,因为连接处理是自动完成的。

回答by ppeterka

I have used it for optimization purposes. Using Native queries means that the ORM mapping is not in place, and instead of JPQL, you use the DB's native syntax. So, as @RasmusFranke also pointed out, if you need something that is not supported by JPA (like when you want to use DB vendor specific extensions, which is conceptually a bad idea, since JPA is all about being DB agnostic, but happens nevertheless. I know...)

我已将其用于优化目的。使用本机查询意味着 ORM 映射没有到位,您使用数据库的本机语法而不是 JPQL。因此,正如@RasmusFranke 还指出的那样,如果您需要 JPA 不支持的东西(例如当您想使用 DB 供应商特定的扩展时,这在概念上是一个坏主意,因为 JPA 完全与 DB 无关,但仍然会发生。 我知道...)

The other effect of this is that by using native queries, only the supplied query is run. No eager fetching of other entities, or other unwanted stuff. This way, if you deal with huge amounts of objects, you can save some heap space.

这样做的另一个影响是,通过使用本机查询,只运行提供的查询。无需急切地获取其他实体或其他不需要的东西。这样,如果您处理大量对象,则可以节省一些堆空间。