Difference between hibernate. properties and hibernate. cfg. xml

Source: Internet
Author: User

 

The Hibernate database connection information is loaded from the configuration file.

The configuration file of hibernate can be in either XML or properties.

1) hibernate. cfg. xml

In addition to the basic hibernate configuration information, you can also specify a persistence class ing file in the XML configuration file, which avoids hard coding of the persistence class configuration file in the program. The default file name of the configuration file in XML format is hibernate. cfg. xml. Location: src/hibernate. cfg. xml.

Example:

<? 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>
<Hibernate-configuration>
<Session-factory>
<! -- Display the executed SQL statement -->
<Property name = "show_ SQL"> true </property>
<! -- Connection string -->
<Property name = "connection. url"> JDBC: mysql: // localhost: 3306/STU </property>
<! -- Database connection username -->
<Property name = "connection. username"> root </property>
<! -- Database User Password -->
<Property name = "connection. Password"> root </property>
<! -- Database-driven -->
<Property name = "connection. driver_class"> com. MySQL. JDBC. Driver </property>
<! -- Select the dialect to use -->
<Property name = "dialect"> org. hibernate. dialect. mysqldialect </property>
<! -- Ing file -->
<Mapping Resource = "com/stuman/domain/admin. HBM. xml"/>
<! -- Ing file -->
<Mapping Resource = "com/stuman/domain/student. HBM. xml"/>
</Session-factory>
</Hibernate-configuration>

 

2) hibernate. Properties

The default file name of the configuration file in properties form is hibernate. properties. The configuration file contains a series of attribute configurations. hibernate will connect to the Database Based on these attributes. Location: src/hibernate. properties.

The configuration file is as follows:

# Specify the driver class used by the database
Hibernate. Connection. driver_class = com. MySQL. JDBC. Driver R
# Specifying database connection strings
Hibernate. Connection. url = JDBC: mysql: // localhost: 3306/DB
# Specify the database connection User Name
Hibernate. Connection. Username = user
# Specify the database connection password
Hibernate. Connection. Password = Password
# Specify the dialect used by the database
Hibernate. dialect = net. SF. hibernate. dialect. mysqldialect
# Specify whether to print SQL statements
Hibernate. show_ SQL = true

 

3)

Properties configuration files and xml configuration files can be used at the same time. When two types of configuration files are used at the same time, the settings in the XML configuration file overwrite the same properties in the properties configuration file.

Source: http://hanshuo528.bokee.com/viewdiary.23028288.html

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.