Hibernate obtains sesssionFacotory and hibernate obtains the session.

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.