Hibernate obtains sesssionFacotory and hibernate obtains the session.
The hibernate version is updated quickly, and the way to obtain sessionFactory is also changing.
1. Common Methods:
public SessionFactory test1(){ Configuration cfg = new Configuration().configure("hibernate.cfg.xml"); SessionFactory sessionFactory = cfg.buildSessionFactory(); return sessionFactory; }
This method is the earliest method. Annotations are not supported before. Annotations are not supported until 3.6.0.Final. 4.0.final expires.
2. annotation
public SessionFactory test2(){ Configuration cfg = new org.hibernate.cfg.AnnotationConfiguration().configure("hibernate.cfg.xml"); SessionFactory sessionFactory = cfg.buildSessionFactory(); return sessionFactory; }
Since the emergence of its own annotation, hibernate first uses this method to support annotation. When 3.6.0.Final is out of date, that is, the method starts to support annotation. When it reaches 4.3.0.Final,
Org. hibernate. cfg. AnnotationConfiguration the entire class does not exist. That is, this method is completely canceled.
3. service Method
public SessionFactory test3(){ Configuration cfg = new Configuration().configure("hibernate.cfg.xml"); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry); return sessionFactory; }
This method has gone through three major versions: 4.0.x.Final 4.1.x.Final 4.2.x.Final. If you want to continue using outdated methods in 4.3.0.Final, there is no buildServiceRegistry () in ServiceRegistryBuilder. Use build () instead.
4. service2
Configuration cfg = new Configuration().configure("hibernate.cfg.xml"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build(); SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry); return sessionFactory;
In the latest 4.3.x.Final version, ga has not been released for version 5. Currently, the test version is also used in this way.
Note:
Previous versions of hibernate3.4 were proxies using cglib (cglib 2.1.3, asm 1.5.3, asm-attr 1.5.3 ).
Use ipvsist for proxy from 3.4 (including 3.4)