How to get instance of SessionFactoryImplementor?
As in title: how can I get the instance of SessionFactoryImplementor
? What I hacked in the code is:
SessionFactoryImplementator sfi
= (SessionFactoryImplementator) session.getSessionFactory();
I don't like it too much and I was wondering if there is any开发者_开发百科 other, more elegant way of acquiring the instance of implementator.
I didn't find any public class that returns the implementor, so your approach is fine. (For example look here, under usage > returned by)
How about:
SessionFactoryImplementor sfi = (SessionFactoryImplementor) ctx.getBean("sessionFactory");
After reading the above comments, I would propose that the original author's code example...
SessionFactoryImplementator sfi = (SessionFactoryImplementator) session.getSessionFactory();
and then the latter re-coding of the same principal:
SessionFactoryImplementor sfi = (SessionFactoryImplementor) ctx.getBean("sessionFactory");
Anyone who is in fact using Spring should ALWAYS defer to the latter approach as there is no way of knowing how Spring3/4's SessionFactoryBean might choose to re-implement "openSession" etc. (or the developer application), but point being that accessing it through a Spring ApplicationContext ensures that you have the "highest-level" wrapped version of the actual SessionFactory, which should implement the greatest number of Hibernate SessionFactory-related interfaces (whether wrapped through AoP or compile-time class-weaving, etc.)
If you know that there is only one Hibernate SessionFactory in your "appCtx", then an even better way would be:
appCtx.getBean(org.hibernate.SessionFactory.class);
精彩评论