HIBERNATE4 Environment Construction and Hibernateutil

Source: Internet
Author: User

This topic to tell, hibernate environment construction, this example uses Hibernate version: Hibernate-release-4.3.8.final. This example jar file provides the download path as:http://download.csdn.net/detail/ma_hoking/8380545. Readers who need it, can download it for themselves.
Extract the downloaded file hibernate-release-4.3.8.final.zip, the file under the Hibernate-release-4.3.8.final\lib\required directory, is the basic jar package required by the environment. At this point, we have the required jar files.
Create a Java Project Hibernate4learn and import the necessary jar files into your project. Because this example needs to connect to the local MySQL database, the new jar file Mysql-connector-java-5.1.7-bin.jar needs to be introduced.
"Reproduced use, please specify the source:http://blog.csdn.net/mahoking"
First of all, the native environment has installed MySQL database, so this article does not focus on its specific installation procedures, readers can refer to this articlehttp://blog.csdn.net/mahoking/article/details/42921511.
The following describes the configuration process for the master configuration file Hibernate.cfg.xml, which is created first, and reads as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration public          "-//hibernate/hibernate configuration DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">          


Configuration using Driver

<property name= "Connection.driver_class" >com.mysql.jdbc.driver</property><property name= " Connection.username ">root</property><property name=" Connection.password ">root</property> <property name= "Connection.url" >jdbc:mysql://127.0.0.1:3306/mhc</property>


Specify the SQL dialect used by the database

<property name= "dialect" >org.hibernate.dialect.MySQLDialect</property>


Specifies whether to output SQL statements in the console when the program is run.
When the Show_sql property is true, indicates that the SQL statement is output in the console and defaults to False. It is recommended that you set true when you debug the program, and then change to False before the publisher, because the output SQL statement affects how quickly the program runs.

<property name= "Show_sql" >true</property>


Configure Hbm2ddl.auto for optional options such as:
Create recreate the database table structure every time hibernate is loaded
Create-drop when loading hibernate is created, exit is delete table structure
Update load hibernate automatically updates database structure
Validation of creating database table structure when validate loading hibernate
<property name= "Hbm2ddl.auto" >update</property>
Specify an Object management mapping file (*.hbm.xml)

<mapping resource= "Com/mahaochen/hibernate/domain/user.hbm.xml"/>


The final complete configuration is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://hibernate.so Urceforge.net/hibernate-configuration-3.0.dtd "> 


Master profile, next we need to manually write the Hibernateutil class, to get the Sessionfactory object, and to manage the session, such as opening and closing the session. First of all, this shows that Hibernate3 and Hibernate4 get sessionfactory in different ways. Because the Buildsessionfactory () method of the configuration is obsolete in Hibernate4, it uses Configuration.buildsessionfactory (serviceregistry Serviceregistry) method. The specific implementations of Hibernate3util and Hibernate4util are described in turn below.

Hibernate3util

Import Org.hibernate.session;import org.hibernate.sessionfactory;import org.hibernate.cfg.configuration;/** * For hibernate version V3 use *  * @author MAHC *  */public class Hibernate3util {private static sessionfactory sessionfactory;p Rivate static Session session;static {//create configuration, which is used to read Hibernate.cfg.xml and complete initialization of configuration config = new Configuration (); Config.configure (); sessionfactory = Config.buildsessionfactory ();} /** * Get sessionfactory *  * @return */public static sessionfactory getsessionfactory () {return sessionfactory;} public static session Getcurrentsession () {session = Sessionfactory.opensession (); return session;} public static void CloseSession (Session session) {if (null! = session) {Session.close ();}}}


Hibernate4util

Import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.boot.registry.standardserviceregistry;import Org.hibernate.boot.registry.standardserviceregistrybuilder;import Org.hibernate.cfg.configuration;import org.hibernate.service.serviceregistry;/** * for hibernate version V4.3.8 * * @author MAHC * */public class Hibernate4util {Priv Ate static sessionfactory sessionfactory;private static Session session;static {//Create configuration, The object is used to read the Hibernate.cfg.xml and completes the initialization of configuration config = new configuration (). Configure (); Standardserviceregistrybuilder SSRB = new Standardserviceregistrybuilder (). Applysettings (Config.getProperties ()); Standardserviceregistry SSR = Ssrb.build (); sessionfactory = Config.buildsessionfactory (SSR);} /** * Get sessionfactory * * @return */public static sessionfactory getsessionfactory () {return sessionfactory;} public static session Getcurrentsession () {session = Sessionfactory.opensession (); return session;} public static void CloseSession (Session seSsion) {if (null! = session) {Session.close ();}}} 


"Reproduced use, please specify the source: http://blog.csdn.net/mahoking "

HIBERNATE4 Environment Construction and Hibernateutil

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.