JPQL查询是否可以与内存中的实体/对象一起使用,并且不会击中数据库?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JPQL语法评估对象的字段,而不是数据库字段。
因此,如果您在数据库中以不同的方式映射了字段名称,或者您的对象在多个表中持续存在,则ORM将正确地将JPQL转换为SQL。
如果对象不在内存中实例化,则弹簧/休眠框架将击中数据库。
该理论是您的对象模型非常复杂,您希望能够对基础数据库或表结构不依赖。因此,JPQL使您可以将您从基础数据库中解脱出来。但是,实际上,它通常不是超级无缝的,您会不时编写本地标语(Plain SQL)。
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.
The Spring/Hibernate framework will hit the database if the object is not in memory to instantiate it.
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.