Hibernate 如何获取SessionFactoryImplementor的实例?
如标题所示:如何获取Hibernate 如何获取SessionFactoryImplementor的实例?,hibernate,Hibernate,如标题所示:如何获取SessionFactoryImplementor的实例?我在代码中入侵的是: SessionFactoryImplementator sfi = (SessionFactoryImplementator) session.getSessionFactory(); 我不太喜欢它,我想知道是否还有其他更优雅的方法来获取实现者的实例。我没有找到任何返回实现者的公共类,所以您的方法很好。(例如,在“用法>返回人”下)如何: SessionFactoryImplement
SessionFactoryImplementor
的实例?我在代码中入侵的是:
SessionFactoryImplementator sfi
= (SessionFactoryImplementator) session.getSessionFactory();
我不太喜欢它,我想知道是否还有其他更优雅的方法来获取实现者的实例。我没有找到任何返回实现者的公共类,所以您的方法很好。(例如,在“用法>返回人”下)如何:
SessionFactoryImplementor sfi = (SessionFactoryImplementor) ctx.getBean("sessionFactory");
在阅读了上述评论之后,我建议原作者的代码示例。。。
SessionFactoryImplementator sfi=(SessionFactoryImplementator)session.getSessionFactory()代码>
然后对同一主体进行后一种重新编码:
SessionFactoryImplementor sfi=(SessionFactoryImplementor)ctx.getBean(“sessionFactory”)代码>
任何实际使用Spring的人都应该始终遵循后一种方法,因为无法知道Spring3/4的SessionFactoryBean如何选择重新实现“openSession”等(或开发人员应用程序),但关键是通过Spring应用程序上下文访问它可以确保您拥有“最高级别”实际SessionFactory的包装版本,应该实现最多数量的Hibernate SessionFactory相关接口(无论是通过AoP包装还是编译时类编织等)
如果您知道“appCtx”中只有一个Hibernate SessionFactory,那么更好的方法是:
appCtx.getBean(org.hibernate.SessionFactory.class)代码>是的。对我来说,这似乎也是唯一的办法。我不喜欢,但我能做什么示例链接断开这与问题中的完全相同,但描述为“我不太喜欢它”。