Hibernate configuration file

Source: Internet
Author: User

Hibernate configuration file

Persistent objects can be in the following three States:

Temporary State (Transient): the object is in the temporary State before it is saved to the database. In this case, the database does not have information about the object. If it is not persistent, object Information in the temporary status will be lost after the program exits. It may be recycled by the garbage collector at any time (the corresponding record is not found in the database and should be new initialization). After the save () method is executed, it becomes a Persistent Object (persistence object ), not included in session management, an object in memory, no ID or cache

Persistent: the object is Persistent after it is saved to the database or loaded from the database and has not been detached from the Session. In this case, the database contains information about this object. Because objects in the persistent state can perform any database operations in the Session, there are corresponding records in the database, which are included in session management. When the cache is cleared (dirty data check), it is synchronized with the database. Memory, cache, and database (ID)

Detached: the object is in the persistent state, but now it has left the Session. Although the object in the split state has an id value, it cannot perform operations on the database. For example, LazyInitalizeException may be thrown when the set attribute of delayed loading is read.

Hibernate overall understanding

Hibernate ing mechanism

Hibernate transaction isolation mechanism (optimistic lock-pessimistic lock)

 

Configuration File

Hibernate also supports dynamic configuration

XML configuration

Configurationcfg = new Configuration (). addResource ("com/clf/Cat. hbm. xml ");

// Annotation Configuration

Configuration cfg = new Configuration (). addClass (com. clf. bean. cat. Class );

You can use a method similar to setProperty ("hibernate. dialect", "org. hibernate. dialect. MySQLDialect") to dynamically set Hibernate parameters. You can also use addProperties (Propertiesp) to add parameters in batches.

General Configuration

The configuration file of Hibernate can be XML or properties. The default configuration file name is hibernate. cfg. xml or hibernate. properties, which is located under classpath. Parameters in the properties file have the hibernate prefix, while the xml file does not

Hibernate. connection. driver_class = com. mysql. jdbc. Driver
Hibernate. connection. url = jdbc: mysql: // localhost: 3306/hibernate? CharacterEncoding = UTF-8
Hibernate. connection. username = root
Hibernate. connection. password = admin
Hibernate. dialect = org. hibernate. dialect. MySQLDialect
Hibernate. show_ SQL = true
Hibernate. hbm2ddl. auto = create
Hibernate. current_session_context_class = thread

 

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

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

Configure data sources

<Session-factory>
<! -- The following is the configuration of JNDI -->
<! -- Data source name -->
<Property name = "connection. datasource"> java: comp/env/jdbc/CENAME </property>

<! -- Data source provider -->
<Property name = "hibernate. jndi. url"> </property>

<! -- Data Source implementation class -->
<Property name = "hibernate. jndi. class"> </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>

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:

<Session-factory>
<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>
......
</Session-factory>

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

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • 3
  • Next Page

Related Article

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.