为什么我们需要创建本机查询? [英] Why do we need to create native query?

查看:190
本文介绍了为什么我们需要创建本机查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


  • entityManager.createQuery( query1);

  • entityManager.createNativeQuery(query2);

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

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?

推荐答案

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

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:


  • 低级别访问,这意味着您可以自己优化和处理映射;使用SQL实际访问数据库表,而使用JPQL访问实体对象;

  • 如果您已经知道SQL,可能不想学习JPQL

  • 您已经用SQL编写了查询,没有资源/时间将它们移植到JPQL

这篇关于为什么我们需要创建本机查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆