查询,本地查询,命名查询和类型查询之间的区别

编程入门 行业动态 更新时间:2024-10-27 10:18:52
本文介绍了查询,本地查询,命名查询和类型查询之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

查询,本机查询,指定查询和类型查询之间有什么区别? 独立查询是否存在,还是仅仅是一个缩写?在我看来,本地查询是用简单的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.

更多推荐

查询,本地查询,命名查询和类型查询之间的区别

本文发布于:2023-10-20 03:20:05,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!