JPQL查询是否可以与内存中的实体/对象一起使用,并且不会击中数据库?

发布于 2025-02-14 01:40:03 字数 249 浏览 1 评论 0原文

JPQL与SQL相似,但在对象,属性和 关系而不是表和列

这实际上是什么意思?当您运行jpql查询时,它可以与内存中的实体/对象一起使用,并且不会达到数据库?

我看到,当您运行JPQL查询时,Hibernate实际上将其转化为列出数据库的SQL。

那么,实际上,本机SQL有什么区别? 可能是通过持久性上下文跟踪使用JPQL检索的实体? 但是,这对于通过本机SQL检索的属性也不成立吗?

JPQL is similar to SQL, but operates on objects, attributes and
relationships instead of tables and columns

what does that actualy mean? that when you run a JPQL query ,it works with Entities/Objects in memory and doesn't hit the database ?

I see that when you run a JPQL query, Hibernate does in fact translate it into SQL that hits the database.

So really what is the difference to native SQL?
Is it maybe that the Entity retrieved with JPQL is tracked by the persistence context?
But doesn't this also hold true for entitities retrieved through native sql ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2025-02-21 01:40:03

那是什么意思?

JPQL语法评估对象的字段,而不是数据库字段。

因此,如果您在数据库中以不同的方式映射了字段名称,或者您的对象在多个表中持续存在,则ORM将正确地将JPQL转换为SQL。

当您运行jpql查询时,它可以与内存中的实体/对象一起使用,并且不会点击数据库?

如果对象不在内存中实例化,则弹簧/休眠框架将击中数据库。

那么,实际上与本机SQL有什么区别?

该理论是您的对象模型非常复杂,您希望能够对基础数据库或表结构不依赖。因此,JPQL使您可以将您从基础数据库中解脱出来。但是,实际上,它通常不是超级无缝的,您会不时编写本地标语(Plain SQL)。

what does that actualy mean?

JPQL syntax evaluates your object's fields rather than the Database fields.

So if you have mapped your field names differently in your database, or if your object is persisted across multiple tables, the ORM will take care of correctly translating your JPQL into SQL.

that when you run a JPQL query ,it works with Entities/Objects in memory and doesn't hit the database ?

The Spring/Hibernate framework will hit the database if the object is not in memory to instantiate it.

So really what is the difference to native SQL?

The theory is once your object model is quite complicated, you want to be able to not have a dependency on the underlying DB or table structure. So JPQL allows you an avenue to decouple you from the underlying DB. In practice however it is not generally super seamless and you will be writing NativeQuery (plain SQL) from time to time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文