Welcome to the Hibernate Native SQL Query example tutorial. We looked into Hibernate Query Language and Hibernate Criteria in earlier articles, today we will look into Hibernate Native SQL query with examples.

欢迎使用Hibernate Native SQL Query示例教程。 我们在之前的文章中研究了Hibernate Query Language和Hibernate Criteria ,今天我们将通过示例研究Hibernate Native SQL查询。

HibernateSQL查询 (Hibernate SQL Query)

Hibernate provide option to execute native SQL queries through the use of SQLQuery object. Hibernate SQL Query is very handy when we have to execute database vendor specific queries that are not supported by Hibernate API. For example query hints or the CONNECT keyword in Oracle Database.

Hibernate提供了通过使用SQLQuery对象执行本机SQL查询的选项。 当我们必须执行Hibernate API不支持的数据库供应商特定查询时,Hibernate SQL Query非常方便。 例如,查询提示或Oracle数据库中的CONNECT关键字。

For normal scenarios, Hibernate SQL query is not the recommended approach because we loose benefits related to hibernate association and hibernate first level cache.

在正常情况下,不建议使用Hibernate SQL查询,因为我们失去了与hibernate关联和hibernate一级缓存有关的好处。

I will use MySQL database and same tables and data setup as used in HQL example, so you should check out that first to understand the tables and corresponding model classes mapping.

我将使用与HQL示例中使用MySQL数据库以及相同的表和数据设置,因此您应该先检查一下以了解表和对应的模型类映射。

Hibernate本机SQL示例 (Hibernate Native SQL Example)

For Hibernate Native SQL Query, we use Session.createSQLQuery(String query) to create the SQLQuery object and execute it. For example, if you want to read all the records from Employee table, we can do it through below code.

对于Hibernate本机SQL查询,我们使用Session.createSQLQuery(String query)创建SQLQuery对象并执行它。 例如,如果您想从Employee表中读取所有记录,我们可以通过以下代码来完成。

// Prep work
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();// Get All Employees
Transaction tx = session.beginTransaction();
SQLQuery query = session.createSQLQuery("select emp_id, emp_name, emp_salary from Employee");
List<Object[]> rows = query.list();
for(Object[] row : rows){Employee emp = new Employee();emp.setId(Long.parseLong(row[0].toString()));emp.setName(row[1].toString());emp.setSalary(Double.parseDouble(row[2].toString()));System.out.println(emp);
}

When we execute above code for the data setup we have, it produces following output.

当我们执行以上代码进行数据设置时,将产生以下输出。

Hibernate: select emp_id, emp_name, emp_salary from Employee
Id= 1, Name= Pankaj, Salary= 100.0, {Address= null}
Id= 2, Name= David, Salary= 200.0, {Address= null}
Id= 3, Name= Lisa, Salary= 300.0, {Address= null}
Id= 4, Name= Jack, Salary= 400.0, {Address= null}

Notice that list() method returns the List of Object array, we need to explicitly parse them to double, long etc. Our Employee and Address classes have following toString() method implementations.

注意list()方法返回Object数组的List,我们需要显式地将它们解析为double,long等。我们的Employee和Address类具有以下toString()方法实现。

@Override
public String toString() {return "Id= " + id + ", Name= " + name + ", Salary= " + salary+ ", {Address= " + address + "}";
}
@Override
public String toString() {return "AddressLine1= " + addressLine1 + ", City=" + city+ ", Zipcode=" + zipcode;
}

Notice that our query is not returning Address data, whereas if we use HQL query "from Employee", it returns the associated table data too.

注意,我们的查询没有返回地址数据,但是如果我们使用HQL查询"from Employee" ,它也会返回关联的表数据。

Hibernate SQL查询addScalar (Hibernate SQL Query addScalar)

Hibernate uses ResultSetMetadata to deduce the type of the columns returned by the query, from performance point of view we can use addScalar() method to define the data type of the column. However we would still get the data in form of Object array.

Hibernate使用ResultSetMetadata来推断查询返回的列的类型,从性能的角度来看,我们可以使用addScalar()方法来定义列的数据类型。 但是,我们仍将以对象数组的形式获取数据。

//Get All Employees - addScalar example
query = session.createSQLQuery("select emp_id, emp_name, emp_salary from Employee").addScalar("emp_id", new LongType()).addScalar("emp_name", new StringType()).addScalar("emp_salary", new DoubleType());
rows = query.list();
for(Object[] row : rows){Employee emp = new Employee();emp.setId(Long.parseLong(row[0].toString()));emp.setName(row[1].toString());emp.setSalary(Double.parseDouble(row[2].toString()));System.out.println(emp);
}

The output generated will be same, however we will see slight performance improvement when the data is huge.

生成的输出将相同,但是当数据巨大时我们将看到轻微的性能改进。

Hibernate本机SQL多个表 (Hibernate Native SQL Multiple Tables)

If we would like to get data from both Employee and Address tables, we can simply write the SQL query for that and parse the result set.

如果我们想同时从Employee表和Address表中获取数据,我们可以简单地为此编写SQL查询并解析结果集。

query = session.createSQLQuery("select e.emp_id, emp_name, emp_salary,address_line1, city, zipcode from Employee e, Address a where a.emp_id=e.emp_id");
rows = query.list();
for(Object[] row : rows){Employee emp = new Employee();emp.setId(Long.parseLong(row[0].toString()));emp.setName(row[1].toString());emp.setSalary(Double.parseDouble(row[2].toString()));Address address = new Address();address.setAddressLine1(row[3].toString());address.setCity(row[4].toString());address.setZipcode(row[5].toString());emp.setAddress(address);System.out.println(emp);
}

For above code, the output produced will be like below.

对于上面的代码,产生的输出将如下所示。

Hibernate: select e.emp_id, emp_name, emp_salary,address_line1, city, zipcode from Employee e, Address a where a.emp_id=e.emp_id
Id= 1, Name= Pankaj, Salary= 100.0, {Address= AddressLine1= Albany Dr, City=San Jose, Zipcode=95129}
Id= 2, Name= David, Salary= 200.0, {Address= AddressLine1= Arques Ave, City=Santa Clara, Zipcode=95051}
Id= 3, Name= Lisa, Salary= 300.0, {Address= AddressLine1= BTM 1st Stage, City=Bangalore, Zipcode=560100}
Id= 4, Name= Jack, Salary= 400.0, {Address= AddressLine1= City Centre, City=New Delhi, Zipcode=100100}

Hibernate本机SQL实体和联接 (Hibernate Native SQL Entity and Join)

We can also use addEntity() and addJoin() methods to fetch the data from associated table using tables join. For example, above data can also be retrieved as below.

我们还可以使用addEntity()addJoin()方法使用表连接从关联表中获取数据。 例如,上面的数据也可以如下检索。

//Join example with addEntity and addJoin
query = session.createSQLQuery("select {e.*}, {a.*} from Employee e join Address a ON e.emp_id=a.emp_id").addEntity("e",Employee.class).addJoin("a","e.address");
rows = query.list();
for (Object[] row : rows) {for(Object obj : row) {System.out.print(obj + "::");}System.out.println("\n");
}
//Above join returns both Employee and Address Objects in the array
for (Object[] row : rows) {Employee e = (Employee) row[0];System.out.println("Employee Info::"+e);Address a = (Address) row[1];System.out.println("Address Info::"+a);
}

{[aliasname].*} is used to return all properties of an entity. When we use addEntity() and addJoin() with join queries like above it returns both the objects, as shown above.

{[aliasname].*}用于返回实体的所有属性。 当我们将addEntity()addJoin()与上面的addJoin()查询一起使用时,它将返回两个对象,如上所示。

Output produced by above code is like below.

上面的代码产生的输出如下。

Hibernate: select e.emp_id as emp_id1_1_0_, e.emp_name as emp_name2_1_0_, e.emp_salary as emp_sala3_1_0_, a.emp_id as emp_id1_0_1_, a.address_line1 as address_2_0_1_, a.city as city3_0_1_, a.zipcode as zipcode4_0_1_ from Employee e join Address a ON e.emp_id=a.emp_id
Id= 1, Name= Pankaj, Salary= 100.0, {Address= AddressLine1= Albany Dr, City=San Jose, Zipcode=95129}::AddressLine1= Albany Dr, City=San Jose, Zipcode=95129::Id= 2, Name= David, Salary= 200.0, {Address= AddressLine1= Arques Ave, City=Santa Clara, Zipcode=95051}::AddressLine1= Arques Ave, City=Santa Clara, Zipcode=95051::Id= 3, Name= Lisa, Salary= 300.0, {Address= AddressLine1= BTM 1st Stage, City=Bangalore, Zipcode=560100}::AddressLine1= BTM 1st Stage, City=Bangalore, Zipcode=560100::Id= 4, Name= Jack, Salary= 400.0, {Address= AddressLine1= City Centre, City=New Delhi, Zipcode=100100}::AddressLine1= City Centre, City=New Delhi, Zipcode=100100::Employee Info::Id= 1, Name= Pankaj, Salary= 100.0, {Address= AddressLine1= Albany Dr, City=San Jose, Zipcode=95129}
Address Info::AddressLine1= Albany Dr, City=San Jose, Zipcode=95129
Employee Info::Id= 2, Name= David, Salary= 200.0, {Address= AddressLine1= Arques Ave, City=Santa Clara, Zipcode=95051}
Address Info::AddressLine1= Arques Ave, City=Santa Clara, Zipcode=95051
Employee Info::Id= 3, Name= Lisa, Salary= 300.0, {Address= AddressLine1= BTM 1st Stage, City=Bangalore, Zipcode=560100}
Address Info::AddressLine1= BTM 1st Stage, City=Bangalore, Zipcode=560100
Employee Info::Id= 4, Name= Jack, Salary= 400.0, {Address= AddressLine1= City Centre, City=New Delhi, Zipcode=100100}
Address Info::AddressLine1= City Centre, City=New Delhi, Zipcode=100100

You can run both the queries in the mysql client and notice that the output produced is same.

您可以在mysql客户端中运行两个查询,并注意产生的输出是相同的。

mysql> select e.emp_id as emp_id1_1_0_, e.emp_name as emp_name2_1_0_, e.emp_salary as emp_sala3_1_0_, a.emp_id as emp_id1_0_1_, a.address_line1 as address_2_0_1_, a.city as city3_0_1_, a.zipcode as zipcode4_0_1_ from Employee e join Address a ON e.emp_id=a.emp_id;
+--------------+----------------+----------------+--------------+----------------+-------------+---------------+
| emp_id1_1_0_ | emp_name2_1_0_ | emp_sala3_1_0_ | emp_id1_0_1_ | address_2_0_1_ | city3_0_1_  | zipcode4_0_1_ |
+--------------+----------------+----------------+--------------+----------------+-------------+---------------+
|            1 | Pankaj         |            100 |            1 | Albany Dr      | San Jose    | 95129         |
|            2 | David          |            200 |            2 | Arques Ave     | Santa Clara | 95051         |
|            3 | Lisa           |            300 |            3 | BTM 1st Stage  | Bangalore   | 560100        |
|            4 | Jack           |            400 |            4 | City Centre    | New Delhi   | 100100        |
+--------------+----------------+----------------+--------------+----------------+-------------+---------------+
4 rows in set (0.00 sec)mysql> select e.emp_id, emp_name, emp_salary,address_line1, city, zipcode from Employee e, Address a where a.emp_id=e.emp_id;
+--------+----------+------------+---------------+-------------+---------+
| emp_id | emp_name | emp_salary | address_line1 | city        | zipcode |
+--------+----------+------------+---------------+-------------+---------+
|      1 | Pankaj   |        100 | Albany Dr     | San Jose    | 95129   |
|      2 | David    |        200 | Arques Ave    | Santa Clara | 95051   |
|      3 | Lisa     |        300 | BTM 1st Stage | Bangalore   | 560100  |
|      4 | Jack     |        400 | City Centre   | New Delhi   | 100100  |
+--------+----------+------------+---------------+-------------+---------+
4 rows in set (0.00 sec)mysql>

具有参数的Hibernate本机SQL查询 (Hibernate Native SQL Query with Parameters)

We can also pass parameters to the Hibernate SQL queries, just like JDBC PreparedStatement. The parameters can be set using the name as well as index, as shown in below example.

我们还可以将参数传递给Hibernate SQL查询,就像JDBC PreparedStatement一样 。 可以使用名称和索引来设置参数,如下例所示。

query = session.createSQLQuery("select emp_id, emp_name, emp_salary from Employee where emp_id = ?");
List<Object[]> empData = query.setLong(0, 1L).list();
for (Object[] row : empData) {Employee emp = new Employee();emp.setId(Long.parseLong(row[0].toString()));emp.setName(row[1].toString());emp.setSalary(Double.parseDouble(row[2].toString()));System.out.println(emp);
}query = session.createSQLQuery("select emp_id, emp_name, emp_salary from Employee where emp_id = :id");
empData = query.setLong("id", 2L).list();
for (Object[] row : empData) {Employee emp = new Employee();emp.setId(Long.parseLong(row[0].toString()));emp.setName(row[1].toString());emp.setSalary(Double.parseDouble(row[2].toString()));System.out.println(emp);
}

Output produced by above code would be:

以上代码产生的输出为:

Hibernate: select emp_id, emp_name, emp_salary from Employee where emp_id = ?
Id= 1, Name= Pankaj, Salary= 100.0, {Address= null}
Hibernate: select emp_id, emp_name, emp_salary from Employee where emp_id = ?
Id= 2, Name= David, Salary= 200.0, {Address= null}

That’s all for a brief introduction of Hibernate SQL Query, you should avoid using it unless you want to execute any database specific queries.

这就是Hibernate SQL查询的简要介绍,除非您要执行任何数据库特定的查询,否则应避免使用它。

翻译自: https://www.journaldev.com/3422/hibernate-native-sql-query-example

Hibernate本机SQL查询示例相关推荐

  1. Hibernate Native SQL查询示例

    Hibernate Native SQL查询示例 欢迎使用Hibernate Native SQL Query示例教程.我们在前面的文章中研究了Hibernate查询语言和Hibernate Crit ...

  2. hibernate的native sql查询

    在我们的hibernate中,除了我们常用的HQL查询以外,还非常好的支持了原生的SQL查询,那么我们既然使用了hibernate,为什么不都采用hibernate推荐的HQL查询语句呢?这是因为HQ ...

  3. hibernate将本地SQL查询结果封装成对象

    hibernate将本地SQL查询结果封装成对象 不知道大家有没有碰过这种情况,迫于很多情况只能用native SQL来查询(如:复杂统计等),然而使用native查询后,结果会被放到object里, ...

  4. sql子查询示例_学习SQL:SQL查询示例

    sql子查询示例 In the previous article we've practiced SQL, and today, we'll continue with a few more SQL ...

  5. Hibernate使用原生SQL查询

    Hibernate使用原生SQL查询   Hibernate提供了原生的SQL语句的查询,通过createSQLQuery(String)和createNativeQuery(String),crea ...

  6. Hibernate sql查询

    http://www.cnblogs.com/kelin1314/archive/2010/09/09/1821897.html Hibernate还支持使用SQL查询,使用SQL查询可以利用某些数据 ...

  7. 删除表的sql命令是什么_【技术干货】30个最适合初学者的SQL查询

    毫无疑问,SQL或结构化查询语言是最流行的编程语言之一,尤其是因为它具有访问和修改数据库中数据的功能.SQL与数据库接口的基本用法是其流行的最重要原因. 关于SQL的最基本方面之一就是查询.基本上,S ...

  8. postgre sql 括字段_【技术干货】30个最适合初学者的SQL查询

    毫无疑问,SQL或结构化查询语言是最流行的编程语言之一,尤其是因为它具有访问和修改数据库中数据的功能.SQL与数据库接口的基本用法是其流行的最重要原因. 关于SQL的最基本方面之一就是查询.基本上,S ...

  9. 用于存储过程的ASP.NET Core Blazor和EF Core原始SQL查询

    目录 介绍 背景 先决条件 使用代码 创建数据库和表 步骤1:创建ASP.NET Core Blazor服务器应用程序 运行测试应用程序 步骤2:安装软件包 连接字符串 步骤3:建立模型类 创建DBC ...

最新文章

  1. Hibernate- 子查询
  2. mysql双向复制(主主模式)
  3. Roman to Integer
  4. MySQL中的浮点数类型和定点数类型
  5. Jerry Wang在SAP社区上获得的徽章
  6. 【更新至2.0】cnbeta 根据评论数提取热喷新闻的js脚本
  7. 翻转课堂实践:让学生转换角色试一试
  8. 20135316王剑桥 linux第六周课实验笔记
  9. oracle11g 端口,navicate 连接 oracle11g精简版监听不到端口和用户密码错误问题
  10. sqlserver2005使用convert转换成各种格式的日期
  11. storm1.x介绍
  12. 第八章、面向对象设计
  13. 加入go行列的一个敲门砖吗----小玩意cs多人即时聊天,没有用数据库的,没有用框架的
  14. YYH的营救计划(NOIP模拟赛Round 6)
  15. 5.(高级)CS效果之:边框border
  16. 科比坠机事故调查报告出炉,历史上那些神秘的空难原来都因为这个!
  17. OSPF多实例路由防环检测功能介绍
  18. ASM磁盘空间假装耗尽,ORA-15041: diskgroup space exhausted
  19. python网络安全论文题目_自动化毕业论文题目119个免费参考
  20. linssh2 sftp读取远端目录,获取远端文件或者目录信息

热门文章

  1. 通过char与varchar的区别,学习可变长的字符类型
  2. windows8.1 plsql连接oracle
  3. Python基础学习----Requests获取url请求时间:
  4. 浅谈C中的malloc和free\\感谢lj_860603,工作了,没时间验证了,觉得挺好的,转载了...
  5. [转载] python tuple类型如何索引_Python基础数据类型——tuple浅析
  6. [转载] python标准库系列教程(三)——operator库详细教程
  7. [转载] Java中的关键字含义以及介绍
  8. Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)
  9. 2.C#面向对象基础属性
  10. C语言 NUL、NULL及eof