HQL语句中不可以出现与表有关的内容,而是对象的属性

实体类(注意配置文件):

package domain;

import java.util.HashSet;
import java.util.Set; //客户实体
public class Customer { private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
//使用set集合,表达一对多关系
private Set<LinkMan> linkMens = new HashSet<LinkMan>(); public Set<LinkMan> getLinkMens() {
return linkMens;
}
public void setLinkMens(Set<LinkMan> linkMens) {
this.linkMens = linkMens;
}
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
}
}

抽取工具类:

package utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtils {
private static SessionFactory sf; static{
//1 创建,调用空参构造
Configuration conf = new Configuration().configure();
//2 根据配置信息,创建 SessionFactory对象
sf = conf.buildSessionFactory();
} //获得session => 获得全新session
public static Session openSession(){
//3 获得session
Session session = sf.openSession(); return session; }
//获得session => 获得与线程绑定的session
public static Session getCurrentSession(){
//3 获得session
Session session = sf.getCurrentSession(); return session;
}
}

测试类:

基本语法:

package hql;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import domain.Customer;
import utils.HibernateUtils; //学习HQL语法
public class Demo { //基本语法
@Test
public void fun1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql = " from domain.Customer ";//完整写法
String hql2 = " from Customer "; //简单写法
String hql3 = " from java.lang.Object "; //查询所有Object对象,即所有对象 Query query = session.createQuery(hql); List list = query.list(); System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//排序
public void fun2(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql1 = " from domain.Customer order by cust_id asc ";//完整写法升序
String hql2 = " from domain.Customer order by cust_id desc ";//完整写法降序 Query query = session.createQuery(hql2); List list = query.list(); System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//条件查询
public void fun3(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql1 = " from domain.Customer where cust_id =? ";//?按所有传值
String hql2 = " from domain.Customer where cust_id = :id ";//:按名称传值 Query query = session.createQuery(hql2); //query.setParameter(0, 5l);
query.setParameter("id", 5l); List list = query.list(); System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//分页查询
public void fun4(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql1 = " from domain.Customer ";//完整写法 Query query = session.createQuery(hql1); //类似SQL中的limit ?,?
// (当前页数-1)*每页条数
query.setFirstResult(0);
query.setMaxResults(2); List list = query.list(); System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//统计查询
//count 计数
//sum 求和
//avg 平均数
//max
//min
public void fun5(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql1 = " select count(*) from domain.Customer ";
String hql2 = " select sum(cust_id) from domain.Customer ";
String hql3 = " select avg(cust_id) from domain.Customer ";
String hql4 = " select max(cust_id) from domain.Customer ";
String hql5 = " select min(cust_id) from domain.Customer "; Query query = session.createQuery(hql3); Number number = (Number) query.uniqueResult(); System.out.println(number);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//投影查询
//查询对象某一个属性
public void fun6(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql1 = " select cust_name from domain.Customer ";
String hql2 = " select cust_name,cust_id from domain.Customer ";
String hql3 = " select new Customer(cust_id,cust_name) from domain.Customer ";
//如果实体类有相应构造方法,也可以执行 Query query = session.createQuery(hql2); List list = query.list(); System.out.println(list); //----------------------------------------------------
tx.commit();
session.close(); }
}

多表查询:

再创建一个实体类:

package domain;

//联系人实体
public class LinkMan {
private Long lkm_id;
private Character lkm_gender;
private String lkm_name;
private String lkm_phone;
private String lkm_email;
private String lkm_qq;
private String lkm_mobile;
private String lkm_memo;
private String lkm_position; // 表达多对一关系
private Customer customer; public Customer getCustomer() {
return customer;
} public void setCustomer(Customer customer) {
this.customer = customer;
} public Long getLkm_id() {
return lkm_id;
} public void setLkm_id(Long lkm_id) {
this.lkm_id = lkm_id;
} public Character getLkm_gender() {
return lkm_gender;
} public void setLkm_gender(Character lkm_gender) {
this.lkm_gender = lkm_gender;
} public String getLkm_name() {
return lkm_name;
} public void setLkm_name(String lkm_name) {
this.lkm_name = lkm_name;
} public String getLkm_phone() {
return lkm_phone;
} public void setLkm_phone(String lkm_phone) {
this.lkm_phone = lkm_phone;
} public String getLkm_email() {
return lkm_email;
} public void setLkm_email(String lkm_email) {
this.lkm_email = lkm_email;
} public String getLkm_qq() {
return lkm_qq;
} public void setLkm_qq(String lkm_qq) {
this.lkm_qq = lkm_qq;
} public String getLkm_mobile() {
return lkm_mobile;
} public void setLkm_mobile(String lkm_mobile) {
this.lkm_mobile = lkm_mobile;
} public String getLkm_memo() {
return lkm_memo;
} public void setLkm_memo(String lkm_memo) {
this.lkm_memo = lkm_memo;
} public String getLkm_position() {
return lkm_position;
} public void setLkm_position(String lkm_position) {
this.lkm_position = lkm_position;
} }

测试类:

package hql;

import java.util.Arrays;
import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import domain.Customer;
import utils.HibernateUtils; //学习HQL语法(不常用) - 多表查询语法
public class Demo2 {
//原生SQL中:
// 交叉连接-笛卡尔积(避免)
// select * from A,B
// 内连接
// |-隐式内连接
// select * from A,B where b.aid = a.id
// |-显式内连接
// select * from A inner join B on b.aid = a.id
// 外连接
// |- 左外
// select * from A left [outer] join B on b.aid = a.id
// |- 右外
// select * from A right [outer] join B on b.aid = a.id
//---------------------------------------------------------------------
//HQL的多表查询
//内连接(迫切)
//外连接
// |-左外(迫切)
// |-右外(迫切) @Test
//HQL 内连接 => 将连接的两端对象分别返回.放到数组中.
public void fun1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql = " from Customer c inner join c.linkMens ";
//c相当于别名
Query query = session.createQuery(hql); List<Object[]> list = query.list(); for(Object[] arr : list){
System.out.println(Arrays.toString(arr));
}
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//HQL 迫切内连接 => 帮我们进行封装.返回值就是一个对象
public void fun2(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql = " from Customer c inner join fetch c.linkMens "; Query query = session.createQuery(hql); List<Customer> list = query.list(); System.out.println(list);
//----------------------------------------------------
tx.commit();
session.close(); } @Test
//HQL 左外连接 => 将连接的两端对象分别返回.放到数组中.
public void fun3(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql = " from Customer c left join c.linkMens "; Query query = session.createQuery(hql); List<Object[]> list = query.list(); for(Object[] arr : list){
System.out.println(Arrays.toString(arr));
}
//----------------------------------------------------
tx.commit();
session.close(); }
@Test
//HQL 右外连接 => 将连接的两端对象分别返回.放到数组中.
public void fun4(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//----------------------------------------------------
String hql = " from Customer c right join c.linkMens "; Query query = session.createQuery(hql); List<Object[]> list = query.list(); for(Object[] arr : list){
System.out.println(Arrays.toString(arr));
}
//----------------------------------------------------
tx.commit();
session.close(); } }

补上内连接与外连接区别:

hibernate框架学习笔记10:HQL查询详解的更多相关文章

  1. hibernate框架学习笔记3:API详解

    Configuration对象: package api; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configur ...

  2. hibernate框架学习笔记2:配置文件详解

    实体类: package domain; public class Customer { private Long cust_id; private String cust_name; private ...

  3. spring框架学习笔记2:配置详解

    简单写一个类做示例: package bean; public class User { private String name; private Integer age; public String ...

  4. struts2框架学习笔记2:配置详解

    核心配置文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC ...

  5. mybatis学习笔记(10)-一对一查询

    mybatis学习笔记(10)-一对一查询 标签: mybatis mybatis学习笔记10-一对一查询 resultType实现 resultMap实现 resultType和resultMap实 ...

  6. IP2——IP地址和子网划分学习笔记之《子网掩码详解》

    2018-05-04 16:21:21   在学习掌握了前面的<进制计数><IP地址详解>这两部分知识后,要学习子网划分,首先就要必须知道子网掩码,只有掌握了子网掩码这部分内容 ...

  7. hibernate框架学习笔记7:HQL查询、Criteria查询简介

    HQL查询:hibernate独有的查询语言 适用于不复杂的多表查询 示例: 实体类: package domain; public class Customer { private Long cus ...

  8. j2ee开发之hibernate框架学习笔记

    hibernate框架技术重点学习笔记 1.针对不同的数据库,有不同的数据库实现类,使其符号对应的数据库? mysqlDaoImpl oracleDaoImpl ... ... 2.对象和表记录的转换 ...

  9. Hibernate框架学习笔记

      Hibernate 是一个 JDO( Java Data Objects)工具.它的工作原理是通过文件把值对象(Java对象)和 数据库表之间建立起一个映射关系,还提供数据查询和获取数据的方法. ...

随机推荐

  1. cookie的设置和获取

    // 创建cookiefunction setCookie(name, value, expires, path, domain, secure) { var cookieText = encodeU ...

  2. 编译器重复定义错误:error C2371: 'SIZE' : redefinition; different basic types

    我们常常会定义自己工程用的数据类型,可能会与Windows的基本数据类型冲突. vs会报重复定义错误:error C2371: 'SIZE' : redefinition; different bas ...

  3. rabbitmq简单收发服务搭建

    消息发送.接收简单代码示例 mq.xml //rabbitmq config spring.rabbitmq.host=ip:host spring.rabbitmq.username= spring ...

  4. Dubbo 新编程模型之外部化配置

    外部化配置(External Configuration) 在Dubbo 注解驱动例子中,无论是服务提供方,还是服务消费方,均需要转配相关配置Bean: @Bean public Applicatio ...

  5. Bzoj2946:[POI2000] 最长公共子串

    题面 求多个串的最长公共子串 Sol 套路,拼在一起,二分答案+后缀数组判定 把大于答案的\(height\)分组,然后计算出一个组内是否有所有串的后缀 由于串只有\(5\)个开个桶就好了 # inc ...

  6. poj-3169Layout

    题意 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相同的.因为奶牛相当 ...

  7. JavaScript的基本操作(一)

    JavaScript中有大量的方法可供我们使用,详情可参考:http://jquery.cuishifeng.cn/这也同时导致我们不可能去记住每一个的用法,且开发者每天都在新添更多的方法,所以要想掌 ...

  8. Docker(二):Dockerfile 使用介绍

    上一篇文章Docker(一):Docker入门教程介绍了 Docker 基本概念,其中镜像.容器和 Dockerfile .我们使用 Dockerfile 定义镜像,依赖镜像来运行容器,因此 Dock ...

  9. 数据处理框架:Pig

    Pig pig 是基于hadoop的一个数据处理框架. MapReduce是使用java开发的.Pig有一套自己的数据处理语言.Pig的数据处理过程要转化为MR 来运行. Pig的数据处理语言是数据流 ...

  10. 记录使用微信小程序的NFC和蓝牙功能读取15693芯片的开发历程

    开发目标: (1) 对于Android手机,直接通过微信小程序调用手机的NFC功能,对15693协议的芯片进行读写操作: (2)对于苹果手机(及没有NFC模块的手机),通过微信小程序的蓝牙功能连接到蓝 ...