一、Hibernate的五种检索方式
(一)具体描述(前两种之前讲过,直接从第三种讲起)
(二)具体实例
1、HQL检索
逻辑图
(1)目录结构
(2)准备实体类
BuyOrder.java
package com.hliedu.hql;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
public class BuyOrder {
// 订单号
private Integer ordId;
// 订单名称
private String orderName;
// 订单总金额
private BigDecimal totalPrice;
// 订单明细
private Set<OrderDetail> orderDetail = new HashSet<OrderDetail>();
public Integer getOrdId() {
return ordId;
}
public void setOrdId(Integer ordId) {
this.ordId = ordId;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public BigDecimal getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(BigDecimal totalPrice) {
this.totalPrice = totalPrice;
}
public Set<OrderDetail> getOrderDetail() {
return orderDetail;
}
public void setOrderDetail(Set<OrderDetail> orderDetail) {
this.orderDetail = orderDetail;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return orderName;
}
// 无参数构造方法
public BuyOrder() {
// TODO Auto-generated constructor stub
}
// 投影查询,对有参对象实例化需要提供带参数的方法
public BuyOrder(String orderName) {
// TODO Auto-generated constructor stub
this.orderName = orderName;
}
}
BuyOrder.hbm.xml
OrderDetail.java
package com.hliedu.hql;
import java.math.BigDecimal;
public class OrderDetail {
//订单明细Id
private Integer detailId;
//商品名称
private String goodName;
//商品单价
private Integer count;
//商品单价
private BigDecimal price;
//订单
private BuyOrder buyOrder;
public Integer getDetailId() {
return detailId;
}
public void setDetailId(Integer detailId) {
this.detailId = detailId;
}
public String getGoodName() {
return goodName;
}
public void setGoodName(String goodName) {
this.goodName = goodName;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public BuyOrder getBuyOrder() {
return buyOrder;
}
public void setBuyOrder(BuyOrder buyOrder) {
this.buyOrder = buyOrder;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return goodName;
}
}
OrderDetail.hbm.xml
HibernateUtil.java
HibernateTest.java
hibernate.cfg.xml添加
(3)概念及实现过程
概念一
实现一
概念二
实现二
--------------------------------------------------------------------------------------------------------------------
概念三
实现三
(无fetch)
(有fetch)
2、QBC检索
3、本地SQL检索
(查询)
(添加)
二、Hibernate3种检索策略
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
三、缓存概述
一级缓存(session)
二级缓存(sessionFactory)
四、集成EhCache
(一)简洁配置过程
(二)详细配置过程
所需jar位置:hibernate-release-5.3.16.Final\hibernate-release-5.3.16.Final\lib\optional\ehcache
ehcache.xml存放位置:hibernate-release-5.3.16.Final\hibernate-release-5.3.16.Final\project\etc\
ehcache.xml配置说明:
在ehcache.xml中找到并删除
1、在hibernate.cfg.xml修改
2、或者在*.hbm.xml修改
(三)代码验证