Hibernate. cfg. xml configuration Summary

Source: Internet
Author: User

The description file of hibernate can be either a properties attribute file or an XML file. The following describes the configuration of hibernate. cfg. xml. The configuration format is as follows:
1. Configure the data source

JDBC or JNDI can be configured in hibernate. cfg. xml. This section describes how to configure the data source.
Hibernate. cfg. xml
<? 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>
<! -- Configure Attributes -->
<! -"True" indicates that the SQL statement sent by hibernate to the database is displayed. -->
<Property name = "show_ SQL"> true </property>
<! -- SQL dialect. MySQL is set here -->
<Property name = "dialect"> net. SF. hibernate. dialect. mysqldialect </property>
<! -- Number of database records read at a time -->
<Property name = "JDBC. fetch_size"> 50 </property>
<! -- Batch Delete the database -->
<Property name = "JDBC. batch_size"> 30 </property>
<! -The following is the configuration of JNDI -->
<! -- Data Source NameJDBC/CENAME Java: COMP/ENV is the default JNDI namespace of Tomcat-->
<Property name = "connection. datasource"> JAVA: COMP/ENV/jdbc/CENAME </property>

<! -- Hibernate connection loading class -->

<Property name = "connection. provider_class"> net. SF. hibernate. Connection. performanceconnectionprovider </property>
<Property name = "dialect"> net. SF. hibernate. dialect. sqlserverdialect </property>
<! -Ing file -->
<Mapping Resource = "com/Amigo/pojo/user. HBM. xml"/>
<Mapping Resource = "com/Amigo/pojo/org. HBM. xml"/>
</Session-factory>
</Hibernate-configuration>

2. c3p0 connection pool
The c3p0 connection pool is the recommended connection pool for hibernate. To use this connection pool, add the c3p0 jar package to classpath. An example of c3p0 connection pool configuration is as follows:

Hibernate. cfg. xml

<? 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 SQL statement used to operate the database -->
<Property name = "show_ SQL"> true </property>
<! -- SQL dialect. MySQL is set here -->
<Property name = "dialect"> net. SF. hibernate. dialect. mysqldialect </property>
<! -- Driver, which describes the configuration of MySQL, SQL Server, and Oracle databases in subsequent chapters -->
<Property name = "connection. driver_class"> ...... </Property>
<! -- Jdbc url -->
<Property name = "connection. url"> ...... </Property>
<! -- Database username -->
<Property name = "connection. username"> User </property>
<! -- Database Password -->
<Property name = "connection. Password"> pass </property>

<Property name = "c3p0. min_size"> 5 </property>
<Property name = "c3p0. max_size"> 20 </property>
<Property name = "c3p0. Timeout"> 1800 </property>
<Property name = "c3p0. max_statements"> 50 </property>

<! -- Object and database table image files -->
<Mapping Resource = "com/Amigo/pojo/user. HBM. xml"/>
<Mapping Resource = "com/Amigo/pojo/org. HBM. xml"/>

</Session-factory>

</Hibernate-configuration>

In the preceding configuration, Hibernate generates a connection based on the configuration file and then submits it to c3p0 for management.

3. proxool connection pool
Proxool is different from c3p0 and DBCP. It generates a connection by itself, so the connection information is stored in the proxool configuration file. When you use it, you need to add the proxool-0.8.3.jar to classespath. Configuration example:

Hibernate. cfg. xml

<? 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 SQL statement used to operate the database -->

<Property name = "show_ SQL"> true </property>

<! -- SQL dialect. MySQL is set here -->

<Property name = "dialect"> net. SF. hibernate. dialect. mysqldialect </property>

<! -Proxool configuration -->

<Property name = "proxool. pool_alias"> pool1 </property>

<Property name = "proxool. xml"> proxoolconf. xml </property>

<Property name = "connection. provider_class"> net. SF. hibernate. Connection. proxoolconnectionprovider </property>

<! -- Object and database table image files -->

<Mapping Resource = "com/Amigo/pojo/user. HBM. xml"/>

<Mapping Resource = "com/Amigo/pojo/org. HBM. xml"/>

</Session-factory>

</Hibernate-configuration>

Compile the proxool configuration file proxoolconf. XML in the same directory of hibernate. cfg. xml. The configuration example of this file is as follows:

Proxoolconf. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<! -- The proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<Something-else-entirely>
<Proxool>
<Alias> pool1 </alias>
<! -- Proxool can only manage connections generated by itself -->

<! -- Driver URL -->

<! -- JDBC: mysql: // localhost: 3306/dbname? Useunicode = true & characterencoding = GBK -->

<Driver-URL>... </Driver-URL>

<! -- Driver Class, eg. com. MySQL. JDBC. Driver -->

<Driver-class>... </Driver-class>
<Driver-Properties>

<! -- Database username. For example, the value is root. -->

<Property name = "user" value = "... "/>

<! -- Database Password. For example, the value is root. -->

<Property name = "password" value = ".... "/>
</Driver-Properties>
<! -- The proxool automatically detects the time interval (in milliseconds) of each connection status. When idle connections are detected, the system immediately recycles the connection status and destroys the connection timeout. -->
<House-keeping-sleep-time> 90000 <! -- Indicates the maximum number of requests waiting in the queue because no idle connections can be allocated. User connections exceeding the number of requests will not be accepted. -->
<Maximum-New-connections> 20 </maximum-New-connections>
<! -- Minimum number of idle connections maintained -->
<Prototype-count> 5 </prototype-count>
<! -- The maximum number of connections allowed. If the connection is exceeded, the requests are placed in the queue. the maximum number of waiting requests is determined by maximum-New-connections. -->
<Maximum-connection-count> 100 </maximum-connection-count>
<! -- Minimum connections -->
<Minimum-connection-count> 10 </minimum-connection-count>
</Proxool>
</Something-else-entirely>

4. DBCP connection pool
In hibernate3.0, DBCP is no longer supported. In hibernate.org, the author of hibernate explicitly pointed out that DBCP has bugs in practice and many blank Connections cannot be released in some cases, therefore, DBCP support is abandoned. To use DBCP, developers also need to add both the commons-pool-1.2.jar and commons-dbcp-1.2.1.jar jar packages to the classpath. DBCP, like c3p0, is connected by hibernate.

The configuration in hibernate2.0 is as follows:

Hibernate. cfg. xml

<? XML version = "1.0" encoding = "UTF-8"?>

<! Doctype hibernate-configuration public

"-// Hibernate/hibernate configuration DTD 2.0 // en"

Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>

<Hibernate-configuration>

<Session-factory>

<! -- Display the SQL statement used to operate the database -->

<Property name = "show_ SQL"> true </property>

<! -- SQL dialect. MySQL is set here -->

<Property name = "dialect"> net. SF. hibernate. dialect. mysqldialect </property>

<! -- Driver, which describes the configuration of MySQL, SQL Server, and Oracle databases in subsequent chapters -->

<Property name = "connection. driver_class"> ...... </Property>

<! -- Jdbc url -->

<Property name = "connection. url"> ...... </Property>

<! -- Database username, eg. Root -->

<Property name = "connection. username">... </Property>

<! -- Database Password, eg. Root -->

<Property name = "connection. Password">... </Property>

<Property name = "DBCP. maxactive"> 100 </property>

<Property name = "DBCP. whenexhaustedaction"> 1 </property>

<Property name = "DBCP. maxwait"> 60000 </property>

<Property name = "DBCP. maxidle"> 10 </property>

<Property name = "DBCP. PS. maxactive"> 100 </property>

<Property name = "DBCP. PS. whenexhaustedaction"> 1 </property>

<Property name = "DBCP. PS. maxwait"> 60000 </property>

<Property name = "DBCP. PS. maxidle"> 10 </property>

<! -- Object and database table image files -->

<Mapping Resource = "com/Amigo/pojo/user. HBM. xml"/>

<Mapping Resource = "com/Amigo/pojo/org. HBM. xml"/>

</Session-factory>

</Hibernate-configuration>

5. MySQL connection Configuration
In hibernate, you can configure many types of databases, such as MySQL, SQL Server, and Oracle. The configuration of MySQL is as follows:

Hibernate. cfg. xml

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

<! -- Configure Attributes -->

<! -"True" indicates that the SQL statement sent by hibernate to the database is displayed. -->

<Property name = "show_ SQL"> true </property>

<! -- SQL dialect. MySQL is set here -->

<Property name = "dialect"> net. SF. hibernate. dialect. mysqldialect </property>

<! -- Number of database records read at a time -->

<Property name = "JDBC. fetch_size"> 50 </property>

<! -- Batch Delete the database -->
<Property name = "JDBC. batch_size"> 30 </property>

<! -- Driver -->

<Property name = "connection. driver_class"> com. MySQL. JDBC. Driver </property>

<! -- Jdbc url -->

<Property name = "connection. url"> JDBC: mysql: // localhost/dbname? Characterencoding = gb2312 </property>

<! -- Database username -->

<Property name = "connection. username"> root </property>

<! -- Database Password -->

<Property name = "connection. Password"> root </property>

<! -Ing file -->

<Mapping Resource = "com/Amigo/pojo/user. HBM. xml"/>

<Mapping Resource = "com/Amigo/pojo/org. HBM. xml"/>
</Session-factory>
</Hibernate-configuration>

The above Driver Class is com. MySQL. JDBC. Driver. You need to add the connector jar package (eg. mysql-connector-java-5.0.4-bin.jar) for MySQL to classpath.

6. SQL server connection Configuration
This section describes the hibernate connection settings of the SQL Server database. Here, only the content of the connection section is provided. The rest is the same as that of 2.2.1.5. The content is as follows:

<! -- Driver -->

<Property name = "connection. driver_class"> net. SourceForge. jtds. JDBC. Driver </property>

<! -- Jdbc url -->

<Property name = "connection. url"> JDBC: jtds: sqlserver: // localhost: 1433; databasename = dbname </property>

<! -- Database username -->

<Property name = "connection. username"> SA </property>

<! -- Database Password -->

<Property name = "connection. Password"> </property>

The driver class in the above example uses the jtds driver class, so the reader needs to add the jar package (eg. jtds-1.2.jar) of jtds to classpath.

7. Oracle connection Configuration
This section describes the hibernate connection settings of the SQL Server database. Here, only the content of the connection section is provided. The rest is the same as that of 2.2.1.5. The content is as follows:

<! -- Driver -->

<Property name = "connection. driver_class"> oracle. JDBC. Driver. oracledriver </property>

<! -- Jdbc url -->

<Property name = "connection. url"> JDBC: oracle: thin: @ localhost: 1521: dbname </property>

<! -- Database username -->

<Property name = "connection. username"> test </property>

<! -- Database Password -->

<Property name = "connection. Password"> test </property>

In the above example, the driver class is Oracle. JDBC. Driver. oracledriver. developers need to add the relevant jar package (ojdbc14.jar) to classpath.

 

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.