The second part is the basic configuration of Hibernate.

Source: Internet
Author: User

Programmable configuration method-1
What if I don't want to configure hbm. xml in cfg. xml? You can use a programmable configuration method in the program, that is, to specify the configuration information in cfg. xml. This method is not recommended. As follows:
Configuration cfg = new Configuration ()
. AddResource ("Item. hbm. xml ")
. AddResource ("Bid. hbm. xml ");
 
An alternative method (sometimes better) is to specify the ing class so that Hibernate can help you find the ing definition file:
Configuration cfg = new Configuration ()
. AddClass (org. hibernate. auction. Item. class)
. AddClass (org. hibernate. auction. Bid. class );
This method eliminates any hard encoding of file names.
Programmable configuration method-2
You can also specify the configuration attributes by programming:
Configuration cfg = new Configuration ()
. AddClass (org. hibernate. auction. Item. class)
. SetProperty ("hibernate. dialect", "org. hibernate. dialect. MySQLInnoDBDialect ")
. SetProperty ("hibernate. connection. datasource", "java: comp/env/jdbc/test ")
. SetProperty ("hibernate. order_updates", "true ");
Other methods that can pass in configuration attributes:
1: properties File
2: xml file
3: Set the Java system property, for example, java-Dproperty = value.
Note the following:
The org. hibernate. cfg. Configuration instance is designed as a startup-time object. Once SessionFactory is created, it is discarded.
 
There are two methods for connecting to the database: JDBC and DataSource.
JDBC Configuration
 
Example:
<Property ame = "connection. driver_class"> oracle. jdbc. driver. OracleDriver </property>
<Property name = "connection. url"> jdbc: oracle: thin: @ localhost: 1521: orcl </property>
<Property name = "connection. username"> S </property>
<Property name = "connection. password"> S </property>
Data source configuration
 

Example:
<Property name = "connection. datasource"> java:/javassDs </property>
Connection Pool c3p0 Configuration
The connection pool implemented by Hibernate is not very good. In the project, it is recommended to use an industrial-level connection pool, for example, c3p0. The Hibernate release package contains c3p0. The following is a basic configuration example:
<Property name = "connection. driver_class"> oracle. jdbc. driver. OracleDriver </property>
<Property name = "connection. url"> jdbc: oracle: thin: @ localhost: 1521: orcl </property>
<Property name = "connection. username"> S </property>
<Property name = "connection. password"> S </property>

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

 

Connection PoolC3p0Configuration
The connection pool implemented by Hibernate is not very good. In the project, it is recommended to use an industrial-level connection pool, for example, c3p0. The Hibernate release package contains c3p0. The following is a basic configuration example:
<Property name = "connection. driver_class"> oracle. jdbc. driver. OracleDriver </property>
<Property name = "connection. url"> jdbc: oracle: thin: @ localhost: 1521: orcl </property>
<Property name = "connection. username"> S </property>
<Property name = "connection. password"> S </property>

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

 

To solve the element naming conflict in the database, the catalog and schema are introduced to solve the problem.
Conceptually, a database system contains multiple Catalog S, and each Catalog contains multiple
Schema, and each Schema contains multiple database objects (tables, views, fields, and so on ).
N is simple and common implementation method is to use the database name as the Catalog name and the user name as the Schema name. The following table lists the supported Catalog and Schema in various database systems:
 
 
 
 
 
 
 
 
 
To enable the application to be transplanted in JDBC transactions and JTA Transaction environments, we recommend that you use the optional Hibernate Transaction API, which encapsulates and hides the underlying system.
You can specify the factory class of a transaction instance by setting the Hibernate Configuration Attribute hibernate. Transaction. factory_class.

There are three standard (built-in) Options:
1: Delegate to database (JDBC) Transaction (default)
Previously: org. hibernate. transaction. JDBCTransactionFactory
In Hibernate4: org. hibernate. engine. transaction. internal. jdbc. JDBCTransactionFactory
2: JTA transaction. If there is a running transaction (for example, the EJB Session Bean method) in the Context Environment, the transaction is delegated to the container for management. Otherwise, A new transaction is started and the Bean-managed transaction is used.
Previously: org. hibernate. transaction. JTATransactionFactory
In Hibernate4: org. hibernate. engine. transaction. internal. jta. JTATransactionFactory
3: JTA transactions delegated to container management
Previously: org. hibernate. transaction. CMTTransactionFactory
Hibernate4
In: org. hibernate. engine. transaction. internal. jta. CMTTransactionFactory
You can also define your own transaction policies (for example, the transaction service for CORBA)
 
 
 
 
 
 

1: Hibernate uses SLF4J for logging. You can output logs to the log framework (NOP, Simple, log4j version 1.2, JDK 1.4 logging, JCL, or logback) based on the binding you selected.
2: You need to add the slf4j-api.jar in the classpath and the JAR file you choose to bind (add the slf4j-log4j12.jar when using Log4J), of course, do not forget to add the log4j own jar package.
 
 
Author: jinnianshilongnian

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.