Hibernate=====HQL实用技术
Hibernate支持三种查询语言:HQL查询、Criteria查询和原生SQL查询
HQL(hibernate Query Language,hibernate查询语言)是一种面向对象查询语言,其中没有表和字段的概念,只有类、对象和属性的概念
语法:
form子句:form +全类名 / form + 类名(查询所有)
select子句:select dept.deptname from Dept
where子句:from dept where deptName='sales';
HQL执行语句
例子:form子句、select子句、where子句以及参数查询
建立实体类
public class Dept {
private Integer deptno;
private String dname;
public Dept() {
}
public Dept(Integer deptno, String dname) {
this.deptno = deptno;
this.dname = dname;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
}
测试方法
package cn; import cn.happy.entity.Dog;
import cn.hib.entity.Dept;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Before;
import org.junit.Test; import java.util.List; /**
* Created by a on 2017/8/20.
*/
public class Test924 {
Configuration cfg;
SessionFactory factory;
Session session;
Transaction tx; @Before
public void beforeTest(){
//创建配置对象
cfg=new Configuration().configure();
//根据配置对象创建SessonFactory
factory=cfg.buildSessionFactory();
//根据SessionFactory创建Session
session=factory.getCurrentSession();
//session=factory.openSession();
//开启事务
tx=session.beginTransaction();
} @Test
public void testlist(){
String hql="from Dept";
Query query=session.createQuery(hql);
List<Dept> list=query.list();
for (Dept dept : list){
System.out.println(dept.getDname());
}
}
@Test
public void selectSomeColumnFromTable(){
String hql="select dept.deptno,dept.dname from Dept";
Query query=session.createQuery(hql);
List<Object[]>list=query.list();
for(Object[] obj : list){
for (Object child : obj){
System.out.println(child);
}
}
}
@Test
public void selectSomesColumnFromTable(){
String hql="select new Dept(dept.deptno,dept.dname) from Dept dept";
Query query=session.createQuery(hql);
List<Dept> list=query.list();
for (Dept dept : list){
System.out.println(dept.getDname());
}
}
@Test
public void selectSomeRecordsFromTable(){
String hql="from dept.deptno,dept.dname from Dept dept where dept.dname='RESEARCH'";
Query query=session.createQuery(hql);
List<Dept> list=query.list();
for (Dept dept : list){
System.out.println(dept.getDname());
}
} //案例:参数查询: 方案一:匿名占位符“?”
@Test
public void selectByConditionNiming(){
String hql="from Dept dept where dname=?";
Query query=session.createQuery(hql);
query.setParameter(0,"RESEARCH");
List<Dept> list=query.list();
for(Dept dept : list){
System.out.println(dept.getDname());
}
} //案例:参数查询:方案二:
@Test
public void selectByConditionParametername(){
String hql="from Dept dept where dname=:dname";
Query query=session.createQuery(hql);
query.setParameter("dname","RESEARCH");
List<Dept> list=query.list();
for(Dept dept : list){
System.out.println(dept.getDname());
}
}
//案例:参数查询:方案三:name 参数名称绑定++++对象属性
@Test
public void selectByConditionParameternameAndObjectAttribute(){
String hql="from Dept dept where dname=:dname";
Dept dd=new Dept();
dd.setDname("RESEARCH");
Query query=session.createQuery(hql);
query.setProperties(dd);
List<Dept> list=query.list();
for(Dept dept : list){
System.out.println(dept.getDname());
}
} }
Hibernate=====HQL实用技术的更多相关文章
- 第五章 HQL实用技术
第五章 HQL实用技术5.1 使用HQL查询语句(面向对象查询语句) 5.1.1 编写HQL语句 5.1.1.1 from子句 例:fr ...
- weblogic10异常:org.hibernate.hql.ast.HqlToken
转自:http://www.programgo.com/article/68682994452/ 在做查询的时候,报出 org.hibernate.QueryException: ClassNotF ...
- SSH整合报错:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped[......]
非常诡异的报错,信息如下:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select count(* ...
- Hibernate HQL查询:
Hibernate HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查 ...
- Hibernate学习笔记-Hibernate HQL查询
Session是持久层操作的基础,相当于JDBC中的Connection,通过Session会话来保存.更新.查找数据.session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库 ...
- 解决Eclipse编译器报错ClassNotFoundException:Org.hibernate.hql.ast.HqlToken
最近开发遇到Eclipse编译器老是报出ClassNotFoundException:Org.hibernate.hql.ast.HqlToken [from User Where id=1] 的错误 ...
- java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode(尼玛,蛋疼的错误)
java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode \-[M ...
- Hibernate HQL详细说明
1. Hibernate HQL详细说明 1.1. hql一个简短的引论 Hibernate它配备了一种非常强大的查询语言.这种语言看起来非常像SQL.但是不要 要对相位的语法结构似,HQL是很有 ...
- WebLogic使用SSH架构部署遇到org.hibernate.hql.internal.ast.HqlTok
其实这个问题在以前就遇到过,当时解决了,但今天在部署一个测试轻应用的时候一直没有想起来,特此记录一下. 这个问题出现在使用WebLogic(我使用的是10.3.5版本)发布SSH架构的应用.在操作数据 ...
随机推荐
- 构建docker镜像
一.通过docker commit命令构建镜像 docker commit 构建镜像可以想象为是将运行的镜像进行重命名另存一份.我们先创建一个容器,并在容器里做出修改,就像修改代码一样,最后再将修改提 ...
- 多线程 NSOpeartion 的使用
NSOperation简介 相对于 GCD ,具有面向对象的特征,比 GCD 更简单易用,代码可读性强 NSOperatioin 单独使用时, 不具有开辟新线程的能力, 只是同步执行操作, 需要配合 ...
- 转载---<html>与<body>
关于根元素html以及body的对比,主要是遇到设置背景色的问题,这里转载张鑫旭的关于html和body对比的文.(直接贴过来,是为了以后自己方便看) 原文地址:http://www.zhangxin ...
- 远程诊断DoIP
目录 远程诊断DoIP Part 1: General information and use case definition DoIP诊断网络架构 诊断连接场景 DoIP之通信建立 DoIP中的一些 ...
- Android 单选按钮(RadioButton)和复选框(CheckBox)的使用
1.RadioButton (1)介绍 (2)单选按钮点击事件的用法 (3)RadioButton与RadioGroup配合使用实现单选题功能 (4)xml布局及使用 <?xml version ...
- php Apache No input file
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] 变为 RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
- 洛谷P2146 [NOI2015]软件包管理器
https://www.luogu.org/problemnew/show/P2146 传送门 简单的树链剖分......维护下当前安装了多少个包......修改后查询下就行了......附上极其丑陋 ...
- Codeforce-1106-D. Lunar New Year and a Wander(DFS遍历+vector存图+set)
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park. The park can be re ...
- ASP.Net Core 发布ABP项目遇到的错误
1.HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 与ASP.NET时代不同,ASP.NET Core不再是由IIS工作 ...
- 网络编程api bind函数细节 select 细节
struct sockaddr_in bindaddr; bindaddr.sin_family = AF_INET; bindaddr.sin_addr.s_addr = htonl(INADDR_ ...