how to lazy load collection when using spring-data-jpa, with hibernate, from an console application

后端 未结 3 1419
生来不讨喜
生来不讨喜 2020-12-29 03:20

I have an small console application and I am using spring-data-jpa with hibernate. I really can not figure out how to lazy initialize collections when using spring-data-jpa

3条回答
  •  囚心锁ツ
    2020-12-29 03:55

    This worked for me:

    @Component
    public class Test {
    
        @Inject 
        OrderDAO orderDAO;
    
        @PersistenceUnit
        private EntityManagerFactory emf;
    
        @PostConstruct
        public void test(){
            EntityManager em= emf.createEntityManager();
            for (Order o:orderDAO.findAll()){
                o=em.find(o.getClass(),o.getId());
                System.out.println(o.getLazyField());
            }
            em.close();
        }
    }
    

提交回复
热议问题