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架构的应用.在操作数据 ...
随机推荐
- go 递归实现快排
package main import ( "fmt" ) func main() { arr := []int{1,2,5,8,7,4,3,6,9,0,12,13,45,78,8 ...
- Linux常用命令--用户管理,文件权限,打包命令等
幕布链接 Linux常用命令--用户管理,文件权限,打包命令等
- 堆排序工具类(适用于top k问题,java泛型实现)
代码如下,作用如标题所述 public class HeapSort { //方法作用:取出list里面的最小的 k 个值 public static <T extends Comparable ...
- logback&slf4j学习笔记
1.Slf4j 1.1.Slf4j简介 SLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统.实际上,SLF ...
- cp命令覆盖不提示
参数说明 -i, --interactive prompt before overwrite (overrides a previous -n option) #文件存在是,交互式提示是否覆盖 - ...
- postgreSQL PL/SQL编程学习笔记(六)——杂七杂八
1 PL/pgSQL Under the Hood This part discusses some implementation details that are frequently import ...
- 181114socke编程
一.Socket Families 地址簇 socket.AF_UNIX socket.AF_INET socket.AF_INET6 二.Socker Types socket.SOCK_STREA ...
- 自动判断PC端、手机端跳往不同的域名JS实现代码
输入相同域名,在pc端和移动端会出现不同的页面效果,一种是用栅格系统实现自适应, 更多的是设计两套不同的模板和两个二级域名或者一个主域名和一个二级域名(就是有区别就可以了); js代码判断浏览器的用户 ...
- CentOS安装pip并修改源为豆瓣源
使用yum进行安装 yum install python-pip 若出现 No package python-pip available. 则解决方法如下: yum -y install epel-r ...
- ggplot你不知道的细节
例一 Michaelis-Menten动力学方程 这个例子中采用出自文献中的一组有关于浮萍氮摄取的数据,共2两个变量8个观测值,其中底物浓度与浮萍的氮取速率之间可以通过M-M动力学方程来进行描述.在这 ...