Hibernatesessionfactory of the Java EE hibernate

Source: Internet
Author: User

Yesterday, an afternoon was plagued by a bug, the final layer of troubleshooting, found to be MyEclipse 2014 auto-generated hibernatesessionfactory have problems.
After observing netizens to provide the automatic generation of hibernatesessionfactory also not the same, it is somewhat strange, because the course project time is tense, this semester too tight bar, the cause of this bug is not discussed, The following is the correct hibernatesessionfactory of the design, and then affixed to the wrong hibernatesessionfactory (and very strange, I do hibernate mapping when the demo is using this error hibernatesessionfactory, none of this problem).

Citations: http://www.cnblogs.com/dyllove98/archive/2013/08/07/3243769.html


I. Environment-dependent

Hibernate 5.1.3 Final

Second, the right hibernatesessionfactory

Package Com.cpms.util;import Org.hibernate.hibernateexception;import Org.hibernate.session;import org.hibernate.cfg.configuration;/** * Configures and provides access to hibernate sessions, tied to the * current thread O  F execution. Follows the Thread Local Session * pattern, see {@link http://hibernate.org/42.html}.     */public class Hibernatesessionfactory {/** * location of hibernate.cfg.xml file.       * location should is on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration file. * The default classpath location of the hibernate config file was * in the default package.        Use #setConfigFile () to update * The location of the configuration file for the current session.    */private static final threadlocal<session> ThreadLocal = new threadlocal<session> ();    private static Org.hibernate.SessionFactory sessionfactory;    private static Configuration Configuration = new configuration (); private static STRing config_file_location = "/hibernate.cfg.xml"; private static String ConfigFile = config_file_location;static {try {configuration.configure (configfile); sessionfacto ry = Configuration.buildsessionfactory ();}    catch (Exception e) {System.err.println ("%%%% Error Creating sessionfactory%%%%"); E.printstacktrace ();}  } private Hibernatesessionfactory () {}/** * Returns the ThreadLocal Session instance.     Lazy Initialize * The <code>SessionFactory</code> if needed. * * @return Session * @throws Hibernateexception */public static session getsession () throws Hibernateex Ception {Session session = (session) Threadlocal.get (); if (session = = NULL | |!session.isopen ()) {if (sessionfacto ry = = null) {rebuildsessionfactory ();} Session = (Sessionfactory! = null)?        Sessionfactory.opensession (): Null;threadlocal.set (session);}    return session; }/** * Rebuild Hibernate session factory * */public static void RebuildsessioNfactory () {try {configuration.configure (configfile); sessionfactory = Configuration.buildsessionfactory ();} catch ( Exception e) {System.err.println ("%%%% Error Creating sessionfactory%%%%"); E.printstacktrace ();}}     /** * Close The single Hibernate session instance.  * * @throws hibernateexception */public static void CloseSession () throws Hibernateexception {Session        Session = (session) Threadlocal.get ();        Threadlocal.set (NULL);        if (session! = NULL) {session.close ();  }}/** * return session factory * */public static org.hibernate.SessionFactory getsessionfactory () {return Sessionfactory;} /** * Return Session Factory * *session factory'll be rebuilded on the next call */public static void S Etconfigfile (String configfile) {hibernatesessionfactory.configfile = Configfile;sessionfactory = null;} /** * Return Hibernate configuration * */public static configuration getconfiguration () {RETUrn configuration;}} 



Third, the hibernatesessionfactory of error

Package Com.cpms.util;import Org.hibernate.hibernateexception;import Org.hibernate.session;import Org.hibernate.cfg.configuration;import Org.hibernate.service.serviceregistry;import Org.hibernate.boot.registry.standardserviceregistrybuilder;//import Org.hibernate.service.ServiceRegistryBuilder;  /** * Configures and provides access to Hibernate sessions, tied to the * current thread of execution. Follows the Thread Local Session * pattern, see {@link http://hibernate.org/42.html}.     */public class Hibernatesessionfactory {/** * location of hibernate.cfg.xml file.       * location should is on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration file. * The default classpath location of the hibernate config file was * in the default package.        Use #setConfigFile () to update * The location of the configuration file for the current session. */private static final threadlocal<session> ThreadLocal = new Threadlocal<seSsion> ();    private static Org.hibernate.SessionFactory sessionfactory;    private static Configuration Configuration = new configuration (); private static Serviceregistry serviceregistry; static {try {configuration.configure (); serviceregistry = new Standardserviceregistrybuilder (). Applysettings ( Configuration.getproperties ()). build (); sessionfactory = Configuration.buildsessionfactory (serviceregistry);}    catch (Exception e) {System.err.println ("%%%% Error Creating sessionfactory%%%%"); E.printstacktrace ();}  } private Hibernatesessionfactory () {}/** * Returns the ThreadLocal Session instance.     Lazy Initialize * The <code>SessionFactory</code> if needed. * * @return Session * @throws Hibernateexception */public static session getsession () throws Hibernateex Ception {Session session = (session) Threadlocal.get (); if (session = = NULL | |!session.isopen ()) {if (sessionfacto ry = = null) {rebuildsessionfactory ();} Session = (SESSionfactory! = null)?        Sessionfactory.opensession (): Null;threadlocal.set (session);}    return session; }/** * Rebuild Hibernate session factory * */public static void Rebuildsessionfactory () {try {configuration.c Onfigure (); serviceregistry = new Standardserviceregistrybuilder (). Applysettings (Configuration.getproperties ()). Build (); sessionfactory = Configuration.buildsessionfactory (serviceregistry);} catch (Exception e) {System.err.println ("%%%% Error Creating sessionfactory%%%%"); E.printstacktrace ();}}     /** * Close The single Hibernate session instance.  * * @throws hibernateexception */public static void CloseSession () throws Hibernateexception {Session        Session = (session) Threadlocal.get ();        Threadlocal.set (NULL);        if (session! = NULL) {session.close ();  }}/** * return session factory * */public static org.hibernate.SessionFactory getsessionfactory () {return Sessionfactory;} /** * return Hibernate configuration * */public static configuration getconfiguration () {return configuration;}} 

Hibernatesessionfactory of Java ee hibernate

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.