代码之家  ›  专栏  ›  技术社区  ›  Tomasz Blachowicz

如何获取SessionFactoryImplementor的实例?

  •  3
  • Tomasz Blachowicz  · 技术社区  · 14 年前

    如标题所示:如何获取 SessionFactoryImplementor ? 我破解的代码是:

    SessionFactoryImplementator  sfi
        = (SessionFactoryImplementator) session.getSessionFactory();
    

    3 回复  |  直到 14 年前
        1
  •  3
  •   Bozho    14 年前

    我没有找到任何返回实现者的公共类,所以您的方法很好。(例如 look here ,使用中返回)

        2
  •  1
  •   tonyloca    13 年前

    怎么样:

    SessionFactoryImplementor sfi = (SessionFactoryImplementor) ctx.getBean("sessionFactory");
    
        3
  •  1
  •   dgarson    10 年前

    在阅读了以上的评论之后,我建议原始作者的代码示例。。。 SessionFactoryImplementator sfi = (SessionFactoryImplementator) session.getSessionFactory(); 然后对同一主体重新编码: SessionFactoryImplementor sfi = (SessionFactoryImplementor) ctx.getBean("sessionFactory"); 实际上使用Spring的任何人都应该遵循后一种方法,因为无法知道Spring3/4的SessionFactoryBean如何选择重新实现“openSession”等(或开发人员应用程序),但重点是,通过Spring ApplicationContext访问它可以确保您拥有实际SessionFactory的“最高级别”包装版本,它应该实现最多的Hibernate SessionFactory相关接口(无论是通过AoP包装还是编译时类编织等)

    如果您知道“appCtx”中只有一个Hibernate SessionFactory,那么更好的方法是: appCtx.getBean(org.hibernate.SessionFactory.class);