查询,本地查询,命名查询和类型查询之间的区别 [英] Difference between query, native query, named query and typed query

查看:158
本文介绍了查询,本地查询,命名查询和类型查询之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查询,本机查询,指定查询和类型查询之间有什么区别? 独立查询是否存在,还是仅仅是一个缩写?在我看来,本地查询是用简单的SQL编写的查询,而命名查询涉及实体(hibernate-mapping)。有人可以简单解释一下吗?

解决方案

查询

b

查询引用JPQL / HQL查询,其语法类似于通常用于执行DML语句(CRUD操作)的SQL。

在JPA中,您可以创建一个查询使用 entityManager.createQuery()。您可以查看 API 以获取更多详细信息。



在Hibernate中,使用 session.createQuery()



原生查询是指实际的sql查询(指实际的数据库对象)。这些查询是sql语句,可以是直接在数据库中使用数据库客户端执行。



JPA: entityManager.createNativeQuery()
Hibernate非JPA实现): session.createSQLQuery()


$ b NamedQuery



类似于定义常量的方式,NamedQuery是通过给定名称来定义查询的方式,您可以在hibernate的映射文件中定义它,或者也可以在实体级别使用注释。
$ b

TypedQuery

TypedQuery可让您选择类型当您创建查询时,因此任何操作都不需要显式转换为预期类型。而正常的 Query API不会返回您期望的确切类型的对象,而您需要投射。


What are the differences between a query, a native query, a named query and a typed query? Does the 'alone-standing' query even exist, or is it just an abbreviation? In my mind, a native Query is a query written in simple sql, whereas a named query relates to entities (hibernate-mapping). Can someone explain this briefly?

解决方案

Query

Query refers to JPQL/HQL query with syntax similar to SQL generally used to execute DML statements(CRUD operations).

In JPA, you can create a query using entityManager.createQuery(). You can look into API for more detail.

In Hibernate, you use session.createQuery()"

NativeQuery

Native query refers to actual sql queries (referring to actual database objects). These queries are the sql statements which can be directly executed in database using a database client.

JPA : entityManager.createNativeQuery() Hibernate (Non-JPA implementation): session.createSQLQuery()

NamedQuery

Similar to how the constant is defined. NamedQuery is the way you define your query by giving it a name. You could define this in mapping file in hibernate or also using annotations at entity level.

TypedQuery

TypedQuery gives you an option to mention the type of entity when you create a query and therefore any operation thereafter does not need an explicit cast to the intended type. Whereas the normal Query API does not return the exact type of Object you expect and you need to cast.

这篇关于查询,本地查询,命名查询和类型查询之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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