SQL 查询、原生查询、命名查询和类型查询的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33236664/
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
Difference between query, native query, named query and typed query
提问by Chris311
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?
查询、本机查询、命名查询和类型查询之间有什么区别?“单独站立”查询是否存在,还是只是一个缩写?在我看来,原生查询是用简单的 sql 编写的查询,而命名查询与实体(休眠映射)相关。有人可以简要解释一下吗?
回答by Abhijith Nagarajan
Query
询问
Query refers to JPQL/HQL query with syntax similar to SQL generally used to execute DML statements(CRUD operations).
查询是指JPQL/HQL查询,语法类似于一般用于执行DML语句(CRUD操作)的SQL。
In JPA, you can create a query using entityManager.createQuery()
. You can look into APIfor more detail.
在 JPA 中,您可以使用entityManager.createQuery()
. 您可以查看API以获取更多详细信息。
In Hibernate, you use session.createQuery()
"
在 Hibernate 中,您使用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.
本机查询是指实际的sql查询(指实际的数据库对象)。这些查询是可以使用数据库客户端直接在数据库中执行的 sql 语句。
JPA : entityManager.createNativeQuery()
Hibernate (Non-JPA implementation): session.createSQLQuery()
JPA:entityManager.createNativeQuery()
休眠(非 JPA 实现):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.
类似于定义常量的方式。NamedQuery 是您通过为其命名来定义查询的方式。您可以在 hibernate 的映射文件中定义它,也可以在实体级别使用注释。
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.
TypedQuery 为您提供了在创建查询时提及实体类型的选项,因此此后的任何操作都不需要显式转换为预期类型。而普通Query
API 不会返回您期望的确切类型的 Object,您需要进行转换。