【Hibernate 多表查询】

HibernateManyTable

public class HibernateManyTable {
    
    //演示hql左连接查询
    @Test
    public void testSelect12() {
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction tx = null;
        try {
            sessionFactory = HibernateUtils.getSessionFactory();
            session = sessionFactory.openSession();
            tx = session.beginTransaction();
            
            //1 创建query对象
            Query query = session.createQuery("from Customer c left outer join fetch c.setLinkMan");
            
            
            List list = query.list();
            
            
            tx.commit();
        }catch(Exception e) {
            e.printStackTrace();
            tx.rollback();
        }finally {
            session.close();
            sessionFactory.close();
        }
    }
    
    //演示hql内连接查询
    @Test
    public void testSelect1() {
        SessionFactory sessionFactory = null;
        Session session = null;
        Transaction tx = null;
        try {
            sessionFactory = HibernateUtils.getSessionFactory();
            session = sessionFactory.openSession();
            tx = session.beginTransaction();
            
            //1 创建query对象
            Query query = session.createQuery("from Customer c inner join c.setLinkMan");
            
            List list = query.list();
            
            
            tx.commit();
        }catch(Exception e) {
            e.printStackTrace();
            tx.rollback();
        }finally {
            session.close();
            sessionFactory.close();
        }
    }
}

 

posted @ 2020-02-10 22:43  一只桔子2233  阅读(114)  评论(0编辑  收藏  举报