首页 > Web开发 > 详细

【Hibernate 检索策略】

时间:2020-02-11 00:01:48      阅读:87      评论:0      收藏:0      [点我收藏+]

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();
        }
    }
}

 

【Hibernate 检索策略】

原文:https://www.cnblogs.com/kikyoqiang/p/12293296.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!