【Hibernate 检索策略】
HibernateDemo2
public class HibernateDemo2 { //演示批量抓取 @Test public void testSelect3() { SessionFactory sessionFactory = null; Session session = null; Transaction tx = null; try { sessionFactory = HibernateUtils.getSessionFactory(); session = sessionFactory.openSession(); tx = session.beginTransaction(); //查询所有客户 Criteria criteria = session.createCriteria(Customer.class); List<Customer> list = criteria.list(); //得到每个客户里面所有的联系人 for (Customer customer : list) { System.out.println(customer.getCid() + "::" + customer.getCustName()); //每个客户里面所有的联系人 Set<LinkMan> setLinkMan = customer.getSetLinkMan(); for (LinkMan linkMan : setLinkMan) { System.out.println(linkMan.getLkm_id() + "::" + linkMan.getLkm_name()); } } tx.commit(); } catch (Exception e) { tx.rollback(); } finally { session.close(); sessionFactory.close(); } } //演示检索策略 @Test public void testSelect2() { SessionFactory sessionFactory = null; Session session = null; Transaction tx = null; try { sessionFactory = HibernateUtils.getSessionFactory(); session = sessionFactory.openSession(); tx = session.beginTransaction(); //根据cid=1客户 //执行get方法之后,是否发送sql语句 //调用get方法马上发送sql语句查询数据库 // Customer customer = session.get(Customer.class, 1); // // System.out.println(customer.getCid()); /* * 1 调用load方法之后,不会马上发送sql语句 * (1)返回对象里面只有 id值 * * 2 得到对象里面不是id的其他值时候才会发送语句 * */ Customer customer = session.load(Customer.class, 2); System.out.println(customer.getCid()); System.out.println(customer.getCustName()); tx.commit(); } catch (Exception e) { tx.rollback(); } finally { session.close(); sessionFactory.close(); } } //演示对象导航查询 @Test public void testSelect1() { SessionFactory sessionFactory = null; Session session = null; Transaction tx = null; try { sessionFactory = HibernateUtils.getSessionFactory(); session = sessionFactory.openSession(); tx = session.beginTransaction(); //根据cid=1客户,再查询这个客户里面所有联系人 Customer customer = session.get(Customer.class, 1); //再查询这个客户里面所有联系人 //直接得到客户里面联系人的set集合 //得到set集合,没有发送语句 Set<LinkMan> linkman = customer.getSetLinkMan(); // 发送语句 System.out.println(linkman.size()); tx.commit(); } catch (Exception e) { tx.rollback(); } finally { session.close(); sessionFactory.close(); } } }
相关推荐
abguorui0 2015-03-08
hhboyggyy 2014-08-25
万星明 2013-06-24
haojusheng 2012-12-04
追求 2011-06-28
平步星云 2011-05-05
chenssss 2011-04-05
simon曦 2011-04-02
nameFay 2011-03-29
阳光丶不锈 2010-12-22
chenssss 2010-11-08
TomasEdision 2010-11-08
神儿童 2010-10-19
xhljames 2010-08-11
xing00 2010-05-10
hhboyggyy 2010-04-08
土豆的小秘密 2008-05-22
DOVEty 2008-05-20
feiyangtianyao 2009-09-25