zoukankan      html  css  js  c++  java
  • SpringData JPA多表查询

    对象导航查询:查询一个对象的同时,通过此对象查询他的关联对象(前提得在实体类中配置它们的关联关系)

    /**
         * 对象导航查询:
         *      默认使用的是延迟加载的形式查询
         *          调用get方法并不会立即发送查询,而是在使用关联对象的时候才会查询
         * 将延迟加载改为立即加载需要修改配置
         *      fetch,需要配置到多表映射关系的注解上
         *      
         *      @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
         *      private Set<LinkMan> linkMans = new HashSet<>(0);
         *
         */
        @Test
        @Transactional // 解决在单元测试中的no session问题(could not initialize proxy - no Session)
        public void testQuery1() {
            // 查询id为1的客户
            Customer c = customerDao.findOne(1L);
            // 对象导航查询此客户下的所有联系人
            Set<LinkMan> linkMans = c.getLinkMans();
            for (LinkMan linkMan : linkMans) {
                System.out.println(linkMan);
            }
        }
    /**
         * 从联系人对象导航查询他的所属客户
         *      默认:立即加载
         */
        @Test
        @Transactional
        public void  testQuery2() {
            LinkMan linkMan = linkManDao.findOne(2L);
            //对象导航查询所属的客户
            Customer customer = linkMan.getCustomer();
            System.out.println(customer);
        }
  • 相关阅读:
    设置tomcat访问根路径
    关于Java抽象类的理解
    JavaIO
    synchronized关键字
    Java线程池
    Codeforces1478F-Nezzar and Nice Beatmap
    Codeforces1477B-Nezzar and Binary String
    Codeforces1476D Journey
    Codeforces1478D Nezzar and Board
    Codeforces Round #697 (Div. 3)G. Strange Beauty
  • 原文地址:https://www.cnblogs.com/roadlandscape/p/12380481.html
Copyright © 2011-2022 走看看