Hibernate4 environment construction and HibernateUtil, hibernateutil

Source: Internet
Author: User

Hibernate4 environment construction and HibernateUtil, hibernateutil

This topic to describe, the establishment of the Hibernate environment, this example uses the Hibernate version: hibernate-release-4.3.8.Final. In this example, the download path is http://download.csdn.net/detail/ma_hoking/8380545. You can download and use it by yourself.
Decompress the downloaded file hibernate-release-4.3.8.final.zip. The files under the hibernate-release-4.3.8.Final \ lib \ required directory are the basic jar packages required by the environment. So far, we have obtained the required jar file.
Create the Java project Hibernate4Learn and import the necessary jar files to the project. Since this example needs to connect to the local MySQL database, you need to introduce a new jar file mysql-connector-java-5.1.7-bin.jar.
[For more information, see http://blog.csdn.net/mahoking]
First of all, the MySQL database has been installed in the local environment, so this article does not focus on the specific installation steps. Readers can refer to this article http://blog.csdn.net/mahoking/article/details/42921511.
Next, we will introduce the configuration process of the main configuration file Hibernate. cfg. xml. First, we will create this file with the following content:

 

<?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">          


Configure 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>


Specifies the SQL dialect used by the database

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


Specifies whether to output SQL statements on the console when the program is running.
If the show_ SQL attribute is true, the SQL statement is output on the console. The default value is false. We recommend that you set it to true when debugging the program, and change it to false before releasing the program, because the output SQL statement will affect the program running speed.

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


Configure hbm2ddl. auto with the following options available:
Create loads hibernate each time and re-creates the database table structure
Create-drop is created when hibernate is loaded. Exit is to delete the table structure
Update load hibernate automatically updates the database structure
Verify the structure of the created database table when loading hibernate in validate.
<Property name = "hbm2ddl. auto"> update </property>
Specify object management ing file (*. hbm. xml)

<mapping resource="com/mahaochen/hibernate/domain/User.hbm.xml"/>


The 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.sourceforge.net/hibernate-configuration-3.0.dtd">  


Next we need to manually write the HibernateUtil class to get the SessionFactory object and manage sessions, such as enabling and disabling sessions. It is explained that Hibernate3 and Hibernate4 obtain SessionFactory in different ways. Because the buildSessionFactory () method of Configuration is outdated in Hibernate4, the Configuration. buildSessionFactory (ServiceRegistry serviceRegistry) method is used. The following describes the implementation of Hibernate3Util and Hibernate4Util in sequence.

Hibernate3Util

Import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration;/*** for Hibernate V3 ** @ author mahc **/public class Hibernate3Util {private static SessionFactory sessionFactory; private static Session session; static {// create Configuration, this object is used to read hibernate. cfg. xml, and complete the Initialization Configuration config = new Configuration (); config. configure (); sessionFactory = config. bui LdSessionFactory ();}/*** 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 V4.3.8 ** @ author mahc **/public class Hibernate4Util {private static SessionFactory sessionFactory; priv Ate static Session session; static {// create Configuration, which is used to read hibernate. cfg. xml, and complete the Initialization 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 Ses SionFactory getSessionFactory () {return sessionFactory;} public static Session getCurrentSession () {session = sessionFactory. openSession (); return session;} public static void closeSession (Session session) {if (null! = Session) {session. close ();}}}


[For more information, see http://blog.csdn.net/mahoking]

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.