package org.rabbitx.hibernate4.searchstrategy;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @author RabbitX
*/
public class SearchStrategyJUnitTest {
private static SessionFactory sessionFactory;
private static Session session;
private static Transaction transaction;
@BeforeClass
public static void beforeClass()
{
String cfgPath = "org/rabbitx/hibernate4/searchstrategy/hibernate.cfg.xml";
Configuration cfg = new Configuration().configure(cfgPath);
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
}
@AfterClass
public static void afterClass()
{
sessionFactory.close();
}
@Before
public void before()
{
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void after()
{
if(session.isOpen())
{
transaction.commit();
session.close();
}
}
/**
* 准备需要的数据
*/
@Test
public void testPrepareData()
{
Customer customer = new Customer("BluceLi");
Order order1 = new Order(100090001);
Order order2 = new Order(100090002);
Order order3 = new Order(100090003);
Order order4 = new Order(100090004);
Order order5 = new Order(100090005);
Order order6 = new Order(100090006);
Order order7 = new Order(100090007);
Order order8 = new Order(100090008);
Order order9 = new Order(100090009);
//设定关联关系
order1.setCustomer(customer);
order2.setCustomer(customer);
order3.setCustomer(customer);
order4.setCustomer(customer);
order5.setCustomer(customer);
order6.setCustomer(customer);
order7.setCustomer(customer);
order8.setCustomer(customer);
order9.setCustomer(customer);
customer.getOrders().add(order1);
customer.getOrders().add(order2);
customer.getOrders().add(order3);
customer.getOrders().add(order4);
customer.getOrders().add(order5);
customer.getOrders().add(order6);
customer.getOrders().add(order7);
customer.getOrders().add(order8);
customer.getOrders().add(order9);
session.save(customer);
session.save(order1);
session.save(order2);
session.save(order3);
session.save(order4);
session.save(order5);
session.save(order6);
session.save(order7);
session.save(order8);
session.save(order9);
}
/**
* 在Customer.hbm.xml文件中class节点不设置lazy属性时,默认值为true。
* <class name="Customer" table="TBL_SEARCH_STRATEGY_CUSTOMER" lazy="true">
* 在此设置下使用load方法检索数据时为延迟检索。
* 执行结果:
* 不发送SQL;
* 返回一个hibernate创建的代理对象(org.rabbitx.hibernate4.searchstrategy.Customer_$$_javassist_0);
*/
@Test
public void testClassLazy001()
{
Customer customer = (Customer) session.load(Customer.class, 1);
//返回结果:org.rabbitx.hibernate4.searchstrategy.Customer_$$_javassist_0
System.out.println(customer.getClass().getName());
System.out.println(customer.getId());
}
/**
* 配置:<class name="Customer" table="TBL_SEARCH_STRATEGY_CUSTOMER">
* 延迟检索情况下,在应用程序第一次访问代理类实例的非 OID 属性时, Hibernate 会初始化代理类实例
*/
@Test
public void testClassLazy002()
{
Customer customer = (Customer) session.load(Customer.class, 1);
System.out.println(customer.getName());
System.out.println(customer.getClass().getName());
}
/**
* 配置:<class name="Customer" table="TBL_SEARCH_STRATEGY_CUSTOMER" lazy="false">
* 立即检索情况下,立即检索数据,hibernate不再创建代理对象。
* 执行结果:
* 发送SQL;
* 返回原对象(org.rabbitx.hibernate4.searchstrategy.Customer);
*/
@Test
public void testClassLazy003()
{
Customer customer = (Customer) session.load(Customer.class, 1);
System.out.println(customer.getId());
//返回结果:org.rabbitx.hibernate4.searchstrategy.Customer
System.out.println(customer.getClass().getName());
}
/**
* 配置:<class name="Customer" table="TBL_SEARCH_STRATEGY_CUSTOMER">
* 使用get方法检索数据
* 执行结果:
* 发送SQL;
* 返回原对象(org.rabbitx.hibernate4.searchstrategy.Customer);
*/
@Test
public void testClassLazy004()
{
Customer customer = (Customer) session.get(Customer.class, 1);
System.out.println(customer.getId());
//返回结果:org.rabbitx.hibernate4.searchstrategy.Customer
System.out.println(customer.getClass().getName());
}
/**
* 配置:<class name="Customer" table="TBL_SEARCH_STRATEGY_CUSTOMER">
* 使用Query 的 list() 方法检索数据
* 执行结果:
* 发送SQL;
* 返回原对象(org.rabbitx.hibernate4.searchstrategy.Customer);
*/
@Test
@SuppressWarnings("unchecked")
public void testClassLazy005()
{
Query query = session.createQuery("from Customer");
List<Customer> customers = query.list();
System.out.println(customers);
}
/**
* 配置:<set name="orders" table="TBL_SEARCH_STRATEGY_ORDER" inverse="true">
* 相当于<set name="orders" table="TBL_SEARCH_STRATEGY_ORDER" inverse="true" lazy="true">
* 默认采用延迟检索,通过查看控制台的SQL语句确定
*/
@Test
public void testSetLazy001()
{
Customer customer = (Customer) session.load(Customer.class, 1);
System.out.println(customer.getOrders().getClass());
}
/**
* 配置:<set name="orders" table="TBL_SEARCH_STRATEGY_ORDER" inverse="true" lazy="false">
* 默认采用立即检索,通过查看控制台的SQL语句确定
*/
@Test
public void testSetLazy002()
{
Customer customer = (Customer) session.load(Customer.class, 1);
System.out.println(customer.getName());
System.out.println(customer.getOrders().getClass());
}
/**
* 配置:<set name="orders" table="TBL_SEARCH_STRATEGY_ORDER" inverse="true" lazy="false">
* 默认采用立即检索,通过查看控制台的SQL语句确定
*/
@Test
public void testSetLazy003()
{
Customer customer = (Customer) session.load(Customer.class, 1);
System.out.println(customer.getName());
System.out.println(customer.getOrders().getClass());
}
}
asdfasdfasdfasdf
最新推荐文章于 2022-03-10 20:39:43 发布