Hibernate中的HQL查询QBC查询,二级缓存的配置,类级别和collection级别

package zhang.hibernate.test8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Junction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import zhang.hibernate.entities.Department;
import zhang.hibernate.entities.Employee;

public class TestHibernate8review {
private SessionFactory sessionFactory = null;
private Session session = null;
private Transaction transaction = null;
@Before
public void init() {
Configuration configuration = new Configuration().configure();

	ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
			.buildServiceRegistry();
	sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	session = sessionFactory.openSession();
	transaction = session.beginTransaction();
}

@After
public void destory() {
	transaction.commit();
	session.close();
	sessionFactory.close();
}
/**
 *hibernate中集合级别的二级缓存:使用hibernate类级别的二级缓存:使用elcache插件来启用hibernate的二级缓存
 * 开启类级别的二级缓存的方法配置:
 * 1.加入实现hibernate实现二级缓存的插件推荐使用elcache
 * 2.在hibernate.cfg.xml中配置如下的:
 *  <!-- 二级缓存的配置 -->
 *	<!-- 设置启用二级缓存 -->
 *	<property name="hibernate.cache.use_second_level_cache">true</property>
 *	<property name="hibernate.cache.use_query_cache">true</property>  
 *	<!-- 设置二级缓存的实现的工具 -->
 *  <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
 * 	<!-- 设置要对那些对象进行二级缓存 -->
 * 在department-hbm.xml文件中配置: <cache usage="read-only" />
 * 
 * <set name="employees" table="ZYK_EMPLOYEES" inverse="true" lazy="true">
 *   <cache usage="read-only" />
 *      <key>
 *          <column name="DEPARTMENTID" />
 *      </key>
 *     <one-to-many class="zhang.hibernate.entities.Employee" />
 * </set>
 */
@Test
public void testcacheSetlevel(){
	Department department =(Department) session.get(Department.class, 80);
	System.out.println(department.getEmployees());
	
	transaction.commit();
	session.close();
	System.out.println("-----------------------");
	session = sessionFactory.openSession();
	transaction = session.beginTransaction();
	
	Department department2 =(Department) session.get(Department.class, 80);
	System.out.println(department2.getEmployees());
	
}
/**
 * Hibernate的二级缓存:使用hibernate类级别的二级缓存:使用elcache插件来启用hibernate的二级缓存
 * 开启类级别的二级缓存的方法配置:
 * 1.加入实现hibernate实现二级缓存的插件推荐使用elcache
 * 2.在hibernate.cfg.xml中配置如下的:
 *  <!-- 二级缓存的配置 -->
 *	<!-- 设置启用二级缓存 -->
 *	<property name="hibernate.cache.use_second_level_cache">true</property>
 *	<property name="hibernate.cache.use_query_cache">true</property>  
 *	<!-- 设置二级缓存的实现的工具 -->
 *  <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
 * 	<!-- 设置要对那些对象进行二级缓存 -->
 * <class-cache class="zhang.hibernate.entities.Employee" usage="read-only" />  
 */
@Test
public void testcache(){
	Employee employee1=(Employee) session.get(Employee.class, 107);
	System.out.println(employee1);
	transaction.commit();
	session.close();
	System.out.println("-----------------------");
	session = sessionFactory.openSession();
	transaction = session.beginTransaction();
	
	Employee employee2=(Employee) session.get(Employee.class, 107);
	System.out.println(employee2);
	
}
/**
 * 本地sql查询
 */
@Test
public void querysql(){
	String sql="select * from Employees";
	List<Employee> list = session.createSQLQuery(sql).list();
	System.out.println(list.size());
}
/**
 * QBC翻页
 */
@Test
public void testQBCpage(){
	int pageNo=3;
	int pageSize=5;
	Criteria criteria=session.createCriteria(Employee.class);
	List<Employee> list = criteria.setFirstResult((pageNo-1)*pageSize).setMaxResults(pageSize).list();
	System.out.println(list);
}
/**
 * qbc中的统计:查询最高的工资,最低工资,和平均工资
 */
@Test
public void testqbcmax(){
	Criteria criteria=session.createCriteria(Employee.class);

// criteria.setProjection(Projections.max(“salary”));
// criteria.setProjection(Projections.min(“salary”));
criteria.setProjection(Projections.avg(“salary”));
List list = criteria.list();
System.out.println(list);
}
/**
* QBC中的and和or的语句的:使用Conjunction
*/
@Test
public void testqbcANDOR(){
Criteria criteria=session.createCriteria(Employee.class);
//and
Conjunction conjunction=Restrictions.conjunction();
Department department =new Department();
department.setDepartmentId(80);
conjunction.add(Restrictions.eq(“department”, department)).add(Restrictions.like(“email”, “A”, MatchMode.ANYWHERE));
conjunction.add(Restrictions.like(“emial”, “B”));
System.out.println(conjunction);
//or
Disjunction disjunction=Restrictions.disjunction();
disjunction.add(Restrictions.like(“email”, “A”));
disjunction.add(Restrictions.gt(“salary”, 5000F));
System.out.println(disjunction);

	criteria.add(disjunction);
	criteria.add(conjunction);
	
	List<Employee> list = criteria.list();
	System.out.println(list);
}
/**
 * QBC查询:
 */
@Test
public void testqbc2(){
	Criteria criteria=session.createCriteria(Employee.class);
	Department department =new Department();
	department.setDepartmentId(80);
	criteria.add(Restrictions.eq("department", department));
	criteria.add(Restrictions.gt("salary", 10000f));
	criteria.add(Restrictions.like("email", "a",MatchMode.ANYWHERE));
	
	List<Employee> list = criteria.list();
	System.out.println(list);
}
@Test
public void testqbc(){
	Criteria criteria=session.createCriteria(Employee.class);
	List<Employee> list = criteria.add(Restrictions.eq("email", "NKOCHHAR")).list();
	System.out.println(list);
}
/**
 * hql的左外连接
 */
@Test
public void testleftjoin(){
	String hql="select distinct d from Department d left join  d.employees";
	List<Department> list = session.createQuery(hql).list();
	System.out.println(list);
	//System.out.println(list.size());
}
/**
 * Hql中的迫切左外链接
 * :去除重复的方法
 * 可以使用linkedhashset进行包装
 * 使用distinct
 */
@Test
public void testleftjoinfetch2(){
	String hql="from Department d left join fetch d.employees";
	List<Department> list = session.createQuery(hql).list();
	List depts=new ArrayList(new LinkedHashSet<>(list));
	System.out.println(depts);
	//System.out.println(depts.size());
}
@Test
public void testleftjoinfetch(){
	String hql="select distinct d from Department d left join fetch d.employees";
	List<Department> list = session.createQuery(hql).list();
	System.out.println(list);
	System.out.println(list.size());
}
/**
 * Hql进行命名查询:
 * 注意点:需要在Employee.hbm.xml中配置自定义的
 * --->XML中因为hql语句中可能包含>或者<所以select语句要放在<![CDATE[]]>中从而避免出现歧义
 * <query name="zhangyukangquery">
 * <![CDATA[select id,name,email from Employee e where e.department=:dept]]></query>
 */
@Test
public void testhqlNamedQuery(){
	Department department=new Department();
	department.setDepartmentId(80);
	Query setEntity = session.getNamedQuery("zhangyukangquery").setEntity("dept", department);
	List<Object[]> list = setEntity.list();
	for(Object[] objs:list){
		System.out.println(Arrays.asList(objs));
	}
}
/**
 * hql的分页查询
 */
@Test
public void testpage(){
	int pageNo=3;
	int pageSize=5;
	List<Employee> list = session.createQuery("From Employee")
									.setFirstResult((pageNo-1)*pageSize)
										.setMaxResults(pageSize).list();
	System.out.println(list);
}
/**
 * hql查询带排序条件的
 */
@Test
public void testorderbyhql(){
	String hql="from Employee e where e.salary>? order by e.salary desc";
	List<Employee> list = session.createQuery(hql).setFloat(0, 1000F).list();
	System.out.println(list);
}
/**
 * 利用部分的属性创建对象,给部分的属性进行复制操作
 */
@Test
public void testcreateEmployeebyfields(){
	String hql="select new Employee(id,name,email) "
			+ "from Employee e where e.department=?";
	Department department=new Department();
	department.setDepartmentId(80);
	Query setEntity = session.createQuery(hql).setEntity(0, department);
	List<Employee> emps=setEntity.list();
	System.out.println(emps);
}
/**
 * 获取部分的属性的值
 */
@Test
public void testgetfield(){
	String hql="select id,name ,salary,email from Employee e "
			+ "where e.department=:dept order by salary";
	Department department =new Department();
	department.setDepartmentId(80);
	Query query = session.createQuery(hql).setEntity("dept", department);
	List<Object[]> list = query.list();
	for(Object[] objs:list){
		System.out.println(Arrays.asList(objs));
	}
}
/**
 * 查询各个部门的最低的工资和最小的的工资
 */
@Test
public void testmaxandminHql(){
	String hql="select min(e.salary),max(e.salary) from Employee e "
			+ "group by e.department";
	List<Object[]>list = session.createQuery(hql).list();
	for(Object[] objs:list){
		System.out.println(Arrays.asList(objs));
	}
}
@Test
public void testhql3(){
	String hql="from Employee e where e.salary>? and e.department=:dept";
	Department department=new Department();
	department.setDepartmentId(80);
	Query query = session.createQuery(hql).setFloat(0, 1000f).setEntity("dept", department);
	List<Employee> list = query.list();
	System.out.println(list);
	for(Employee emp:list){
		System.out.println(emp.getSalary()+", "+emp.getName()+" , "+emp.getPhoneNumber());
	}
}
/**
 * 	hql查询:带占位符的hql查询
 */
@Test
public void testhql2(){
	String hql="from Employee e where e.department=:dept order by salary desc";
	Department department =new Department();
	department.setDepartmentId(80);
	List<Employee> list = session.createQuery(hql).setEntity("dept", department).list();
	System.out.println(list);
}
@Test
public void testhql1(){
	String hql="from Employee";
	List<Employee> list = session.createQuery(hql).list();
	System.out.println(list);
}
@Test
public void testsession(){
	System.out.println(session);
}
@Test
public void testget(){
	Employee employee = (Employee) session.get(Employee.class, 107);
	System.out.println(employee);
}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值