什么是hql
hql 是hibernate查询语言 hibernate query language 是一种接近sql的查询语言。
hql与sql的区别?
HQL
HQL的类名和属性区分大小写,关键字不区分大小写。?占位符从下标0开始计算位置支持:命名参数hql是面向对象的查询语言
SQL
SQL的表名和列名不区分大小写。?占位符从顺序1开始计算位置不支持:命名参数SQL是面向结构查询语言
示列代码
命名参数
// 命名参数
@Test
public void listfour
() {
String hql
= "select u.userName,u.realName from User u where u.userName=:uname";
Session session
= SessionFactoryUtil.getSession
();
Transaction transaction
= session.beginTransaction
();
// 创建批量对象
(单个对象
)
Query query
= session.createQuery
(hql
);
query.setString
("uname",
"华哥");
// 运行批量对象
List
<Object
[]> list
= query.list
();
for (Object
[] u
: list
) {
System.out.println
(Arrays.toString
(u
));
}
transaction.commit
();
SessionFactoryUtil.closeSession
();
}
连接查询
// 连接查询
@Test
public void listsix
() {
String hql
= "select o.orderNo,oi.quantity from Order o,OrderItem oi "
+
" where o=oi.order and o.orderId=:orderid";
Session session
= SessionFactoryUtil.getSession
();
Transaction transaction
= session.beginTransaction
();
// 创建批量对象
Query query
= session.createQuery
(hql
);
query.setInteger
("orderid", 4
);
// 运行这个批量对象
List
<Object
[]> list
= query.list
();
for (Object
[] u
: list
) {
System.out.println
(Arrays.toString
(u
));
}
transaction.commit
();
SessionFactoryUtil.closeSession
();
}