I have in my web application some java beans (EJB 3.1) that make use of Hibernate SessionFactory and therefore I need to centralize the creation of this object since I just need one sessionFactory for the whole application. I mean, the following call:
SessionFactory sessionFactory = AnnotationConfiguration().configure().buidSessionFactory;开发者_如何学运维
I though to create a HibernateHelper class and put the method there, getting new sessions within the beans (because Hibernate sessions are lightweight objects) but then I don't know how to "force" such a class to be initialized together with the application (and only once).
Some insights?
Thanks a lot!
This is very similar to another question: First Hibernate project where to place addAnnotatedClass() .
I would just add that if you are already using EJB 3.1, you are probably already using a Java EE Application Server (like JBoss AS). In this case, I would recommend not to use Hibernate directly, but use JPA. This way, the AS takes care of initializing the "entity manager" (like a Hibernate Session) for you once your application is deployed. You don't have to worry about this plumbing code.
精彩评论