/**
Author: willpower
Source: rifoo Technology (http://www.rifoo.com)
Date: 2006-06
Remarks: Reprinted please keep the above statement
**/
Today, let's take a look at hibernate integration and configuration.
For other Java persistence solutions, it is very easy to integrate hibernate into a Java application.
Compared with other solutions, Hibernate designers avoid more common defects and problems and create a lightweight but powerful framework.
Lightweight, that is, we do not need a specific J2EE container or framework to run hibernate, in fact, only j2se 1.2 or later is required for hibernate 3 (the latest annotation function annotations must be supported by j2se 5.0 ).
If one application uses hibernate 2, it is very easy to port from Hibernate 2 to hibernate 3.
Hibernate 3 is not fully backward compatible, but most of the changes are some additional features that we can integrate into existing systems. The Hibernate developer provides the implementation of the core object of hibernate 2 in hibernate 3, so that it can be backward compatible with the method of hibernate 2. In chapter 1 of this book, we will discuss in detail more differences and differences between hibernate 2 and hibernate 3.
One of the most critical features of hibernate design is as few "intrusions" as possible. As we all know, EJB is a highly invasive framework. Therefore, Hibernate is better than EJB in this regard.
Integrate hibernate into Java applications
We can directly call hibernate in a Java application or access hibernate through other frameworks. Hibernate supports Java Management extension (JMX), Java connector framework (JCA), and Java Naming Directory Interface (JNDI) standards.
With JMX, We can configure hibernate at runtime. Hibernate can be deployed as a JCA connector, and we can use JNDI in the application to obtain a session factory of hibernate.
In addition, Hibernate uses a standard JDBC database driver to access relational databases. Hibernate is not a replacement of JDBC, but a layer encapsulated on JDBC.
In addition to standard Java APIs, many Java Web and application framework are integrated with hibernate. For example, Spring framework provides excellent hibernate integration. By encapsulating it at the underlying layer, it provides general support for some persistent objects, a collection of persistent exceptions, and transaction management. We can start with spring's hibernatetemplate class. For more information about spring, visit its official website http://www.springframework.org
No matter how you access hibernat, we need to create a hibernate configuration file. Then, create a hibernate session factory in the configuration file ). Once we have a session factory, our application can request a session from this factory (the session here has a different meaning from the general http session ). Our applications will use hibernate sessions to manage persistent objects.
Later, we will discuss in detail how to use the configuration class to set hibernate instances for all Java applications.
Deploying hibernate
To integrate hibernate into our applications, we must first use some Java class libraries.
The first class library is the jdbc jar file. No matter which type of relational database, Oracle, MSSQL, or MySQL provides its own JDBC jar, we can choose the one we use. In hibernate, there is the concept of dialect, which means that different databases provide their own SQL extensions in addition to standard SQL. For example, Oracle has PLSQL, MSSQL has T-SQL, etc. These differences are understood in hibernate by the concept of dialect. We will discuss the detailed use of dialect later. In addition, the FAQ on the official hibernate website also provides solutions specifically for the database questions of different vendors.
If our JDBC version is old and we still need to use hibernate, We must disable two special features of JDBC 2: Batch update and scrollable result set. (Here I will explain that the former is used for batch update operations, and the latter is mainly used for calling stored procedures ). We can make the following settings in the Configuration:Hibernate. JDBC. batch_size = 0
Hibernate. JDBC. use_scrollable_resultsets = false
After the JDBC driver is configured, the next step is to deploy hibernate3.jar to our application.
Class Library required to run hibernate3
After downloading the release package of hibernate3, we can see a lib directory, which contains many class libraries (that is, Java jar files ), open the README file, which clearly describes what are necessary (such as cglib and dom4j) and what are optional (such as Oscache and log4j ). Some are required during compilation and some are required during runtime. Let's take a closer look at the file.
Next, paste the content of the LIB/readme file of hibernate3 for your convenience:
Ehcache-1.1.jar (1.1)
-Ehcache Cache
-Runtime, optional (required if no other cache provider is set)
Versioncheck. jar (1.0)
-Version Checker
-Buildtime
A swarmcache-1.0rc2.jar (1.0rc2)
-Runtime, optional
-Runtime, optional
Jboss-cache.jar (1.2.2 alpha)
-Treecache clustered Cache
-Runtime, optional
Jgroups-2.2.7.jar (2.2.7)
-Jgroups multicast Library
-Runtime, optional (required by replicated caches)
JTA. Jar (unknown)
-Standard JTA API
-Runtime, required for standalone operation (outside Application Server)
Xml-apis.jar (unknown)
-Standard JAXP API
-Runtime, some SAX Parser is required
Commons-logging-1.0.4.jar (1.0.4)
-Commons Logging
-Runtime, required
C3p0-0.8.5.2.jar (0.8.5.2)
-C3p0 JDBC connection pool
-Runtime, optional
Connector. Jar (unknown)
-Standard JCA API
-Runtime, optional
Jboss-system.jar (unknown)
-Unknown name
-Runtime, optional (required by treecache)
Asm-attrs.jar (unknown)
-ASM bytecode Library
-Runtime, required
Jacc-1_0-fr.jar (1.0-fr)
-Jacc Library
-Runtime, optional
Checkstyle-all.jar (unknown)
-Checkstyle
-Buildtime
Log4j-1.2.9.jar (1. 2)
-Log4j Library
-Runtime, optional
Junit-3.8.1.jar (3.8.1)
-JUnit test framework
-Buildtime
Dom4j-1.6.jar (1.6)
-Xml configuration & Mapping parser
-Runtime, required
Antlr-2.7.5H3.jar (2.7.5h3)
-Another tool for language recognition (patched with proper context classloading)
-Runtime
Cglib-2.1.jar (2.1.0)
-Cglib bytecode Generator
-Runtime, required
Jboss-common.jar (unknown)
-Unknown name
-Runtime, optional (required by treecache)
Oscache-2.1.jar (2.1)
-Opensymphony Oscache
-Runtime, optional
ASM. Jar (unknown)
-ASM bytecode Library
-Runtime, required
Ant-launcher-1.6.3.jar (1.6.3)
-Ant Launcher
-Buildtime
JAAS. Jar (unknown)
-Standard JAAS API
-Runtime, optional (required by JCA)
Jdbc2_0-stdext.jar (2.0)
-Standard extension JDBC APIs
-Runtime, required for standalone operation (outside Application Server)
Ant-antlr-1.6.3.jar (1.6.3)
-Ant anlr support
-Buildtime
Jboss-jmx.jar (unknown)
-Unknown name
-Runtime, optional (required by treecache)
Cleanimports. Jar (unknown)
-Cleanimports
-Buildtime
Xerces-2.6.2.jar (2.6.2)
-SAX Parser
-Runtime, required for JDK <1.4
Jaxen-1.1-beta-4.jar (1.1-beta-4)
-Jaxen-universal Java XPath Engine
-Runtime, required if you want to deserialize a configuration to improve Startup Performance
Ant-junit-1.6.3.jar (1.6.3)
-Ant JUnit support
-Buildtime
Ant-swing-1.6.3.jar (1.6.3)
-Ant swing support
-Buildtime
Commons-collections-2.1.1.jar (2.1.1)
-Commons collections
-Runtime, required
Ant-1.6.3.jar (1.6.3)
-Ant Core
-Buildtime
Proxool-0.8.3.jar (0.8.3)
-Proxool JDBC connection pool
-Runtime, optional
Concurrent-1.3.2.jar (1.3.2)
-Unknown name
-Runtime, optional (required by treecache)