Hibernate学习笔记----检索策略

Customer.java

package cn.limbo.hibernate.strategy;

import java.util.HashSet;
import java.util.Set;

public class Customer {
	
	private Integer customerId;
	private String customerName;
	
	private Set orders = new HashSet<>();
	public void setOrders(Set orders) {
		this.orders = orders;
	}
	public Set getOrders() {
		return orders;
	}
	public Integer getCustomerId() {
		return customerId;
	}
	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
}
Order.java
package cn.limbo.hibernate.strategy;

public class Order {
	private Integer OrderId;
	private String OrderName;
	
	private Customer customer;

	public Integer getOrderId() {
		return OrderId;
	}

	public void setOrderId(Integer orderId) {
		OrderId = orderId;
	}

	public String getOrderName() {
		return OrderName;
	}

	public void setOrderName(String orderName) {
		OrderName = orderName;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	@Override
	public String toString() {
		return "Order [OrderId=" + OrderId + ", OrderName=" + OrderName + ", customer=" + customer + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((OrderId == null) ? 0 : OrderId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Order other = (Order) obj;
		if (OrderId == null) {
			if (other.OrderId != null)
				return false;
		} else if (!OrderId.equals(other.OrderId))
			return false;
		return true;
	}
	
	
}

Customer.hbm.xml





    
        
            
            
        
        
            
               
        
            
                
            
            
        
    

Order.hbm.xml





    
        
            
            
        
        
            
        
        
    

Junit.java

package cn.limbo.hibernate.strategy;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Junit {
	
	private Session session;
	private SessionFactory sessionFactory;
	private Transaction transaction;
	
	@Before
	public void init()
	{
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
				applySettings(configuration.getProperties()).build();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}
	
	@After
	public void destroy()
	{
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
	
	@Test
	public void testClassLevelStrategy()
	{
		Customer customer = (Customer) session.load(Customer.class, 1);
		System.out.println(customer.getClass());
	}
	
	@Test
	public void testOneToManyLevelStrategy()
	{
		Customer customer = (Customer) session.get(Customer.class, 1);
		System.out.println(customer.getCustomerName());
		System.out.println(customer.getOrders().size());
		
		
		Order order = new Order();
		order.setOrderId(1);
		System.out.println(customer.getOrders().contains(order));
		//有时候需要初始化集合的时候显式调用它
		Hibernate.initialize(customer.getOrders());
		//-------------set的lazy属性---------------
		//1.  1-n 或者说 n-n的集合属性默认使用懒加载检索策略
		//2.  可以通过设置set的lazy属性设置默认的检索策略,默认true,不建议设置为false;
		//3.  lazy还可以设置为extra。增强延迟检索,该取值会尽可能延迟集合初始化的时机
	}

	@Test
	public void testSetBatchSize()
	{
		List customers = session.createQuery("FROM Customer").list();
		System.out.println(customers.size());
		
		for(Customer customer : customers)
		{
			if(customer.getOrders() !=null)
			{
				System.out.println(customer.getOrders().size());
			}
		}
		//set属性的batch-size属性:可以设定一次初始化set集合的数量,批量检索能减少SELECT语句的数目
		//提高延迟检索或立即检索的运行性能
	}
	
	@Test
	public void testFetch()
	{
		List customers = session.createQuery("From Customer").list();
		System.out.println(customers.size());
		for(Customer customer:customers)
		{
			if(customer.getOrders() != null)
			{
				System.out.println(customer.getOrders().size());
			}
		}
		
		//set集合的fetch属性:确定初始化orders的集合的方式
		//1.默认值为select,通过正常的方式来初始化set元素
		//2.可以取值为subselect,通过子查询的方式来初始化所有的set集合,子查询
		//作为where子句的in的条件出现,子查询所有一的一端的ID,此时lazy有效
		//但是batch-size失效
		//3.若取值为join,则
		//3.1在加载一的一端的对象时,使用迫切左外连接(使用左外连接进行查询,且吧集合属性进行初始化)的方式检索n的一端的集合属性
		//3.2忽略lazy属性
		//3.3HQL查询忽略fetch=join的取值
	}
	
	@Test
	public void testSetFetch2()
	{
		Customer customer  = (Customer) session.get(Customer.class,1);
		System.out.println(customer.getOrders().size());
	}
	
	@Test
	public void testManyToOneStrategy()
	{
//		Order order = (Order) session.get(Order.class, 1);
//		System.out.println(order.getCustomer().getCustomerName());
		List orders = session.createQuery("From Order o").list();
		for(Order order:orders)
		{
			if(order.getCustomer() != null)
			{
				System.out.println(order.getCustomer().getCustomerName());
			}
		}
		//1.lazy取值为proxy和false分别代表对应的属性采用延迟检索和立即检索
		//2.fetch取值为join表示使用迫切左外连接的方式初始化n关联的一的一端的属性,忽略lazy属性
		//3.batch-size,该属性需要设置在一的一端的class元素的中如
		//
		//作用:一次初始化一这一段代理对象的个数
	}
}




你可能感兴趣的:(Hibernate)