Configuration Object Creation
To create a sessionfactory, you first create a configuration object.
This object is to read some of hibernate's configuration information.
By default, hibernate loads the Hibernate.cfg.xml file into the ClassPath directory.
Here is a continuation of the previous example:
[Hibernate series-] 1. Download and try Hibernate (MySQL and Oracle configuration) development in Eclipse.
This configuration file can be in a variety of ways, XML, can be properties, or directly in the code to write configuration.
Mode 1. Put Hibernate.cfg.xml in the SRC directory, similar to the example in the previous article
Mode 2. Put hibernate.properties in src directory
The contents are as follows:
hibernate.dialect=org.hibernate.dialect.mysqldialecthibernate.connection.driver_class= Com.mysql.jdbc.driverhibernate.connection.url=jdbc:mysql://localhost:3306/testhibernate.connection.username= Roothibernate.connection.password=123456#hibernate.hbm2ddl.auto=create
As you can see, the User.hbm.xml configuration cannot be added in this way, so you can add it in your code:
Configuration configuration = new configuration (). AddResource ("Com/oscar999/usr.hbm.xml");
Mode 3. Can be set directly in the code, like
Configuration configuration = new configuration (). AddResource ("Com/oscar999/usr.hbm.xml"). SetProperty (" Hibernate.connection.driver_class "," Com.mysql.jdbc.Driver "). SetProperty (" Hibernate.connection.url "," Jdbc:mysql ://localhost:3306/test "). SetProperty (" Hibernate.connection.username "," root "). SetProperty (" Hibernate.connection.password "," 123456 "). SetProperty (" Hibernate.dialect "," Org.hibernate.dialect.MySQLDialect ") . SetProperty ("Hibernate.hbm2ddl.auto", "Update");
You can also pass
Configuration configuration = new configuration (). addclass (Com.oscar999.Usr.class)
Add a mapping file.
In general, adding hibernate.cfg.xml will be more common,. properties and. XML can coexist.
In addition, you can do this if you do not want to use the default file name:
File File = new file ("Src/com/oscar999/myhibernate.xml"); Configuration config = new configuration (); Config.configure (file);
creation of Sessionfactory objects
After the Configuration is created, the next step is to create the sessionfactory.
In Hibernate 3, the way to create Sessionfactory is:
Sessionfactory sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
But in Hibernate 4, this approach is obsolete.
The current recommended use is as follows:
Serviceregistry serviceregistry = new Standardserviceregistrybuilder (). Applysettings (Configuration.getproperties () ). build (); Sessionfactory sessionfactory = configuration.buildsessionfactory (serviceregistry);
As for why to use this method, you can refer to:
Http://planet.jboss.org/post/hibernate_orm_service_registry
Use of Session
Sessionfactory have, then simple, directly paste an example
Configuration configuration = new configuration (). addclass (Com.oscar999.Usr.class); Serviceregistry serviceregistry = new Standardserviceregistrybuilder (). Applysettings (Configuration.getproperties () ). build (); Sessionfactory sessionfactory = configuration.buildsessionfactory (serviceregistry); Session session = Sessionfactory.opensession (); Session.begintransaction (); Session.save (New USR ("UESR3")); Session.gettransaction (). commit (); Session.close (); Sessionfactory.close ();
It's important to remember to close the session and Sessionfactory