I. Introduction
When the technology of the database hierarchy just exposed to Java, the JDBC is used, and the subsequent project is getting bigger and bigger. I feel that JDBC is connected everywhere, or uses a direct SQL statement, modification or project change is inconvenient, so it is intended. Write a program based on JDBC-based procedural, all access is converted to JDBC code through this program to access the database.
Client
DBACCESS
JDBC
DB
Client
This technique has brought very changed to development work. All the operations of the database becomes the operation of an object, I use the Java language-specific Reflection mechanism, dynamically obtain a JavaBean property Then correspond to the table of maping a database, so that the code of the client is very simple, for example:
DBACCESS DBA = New DBACCESS ();
Foo f = new foo ();
F.setbar (abar);
... // Setting Properties for Class Foo
DBA.INSERT (f);
The operation of the inserted database can be completed as long as a simple step is a few steps. In fact, I am here to assume some settings, such as most Java types such as int, long, double, string, I all mapping became the String type of the database, and in order to know the Table name corresponding to a JavaBean, and the name of the primary key, I specially This information is recorded in JavaBean. Since there is no XML technology to configure this information, I have made my project management is very troublesome.
Hibernate, it is actually an O / R mapping tool, which uses XML technology, Java Reflection technology, etc., and the basic principle I think I feel similar to the classes I do.
Analysis of Alternative O / R Mapping Tools ---- Hibernate
The reason why hibernate is a more alternative, because the current industry quarrel is a JDO technology, JDO seems to be a standard, JBoss has a Doo, everyone is fighting to become the fact, but in the world Another foreign area, some program masters silently maintain Hibernate technology. Hibernate technology is also compliant with ODMG standards, and its outstanding derived from continuous upgrades, continuous maintenance, modifications. And the perfect document, the popular forum is very popular. It is also important to have its easy-to-use ease of use.
Here I will give a relatively shallow example, which is equivalent to the HelloWorld program we know. But this first step is also very important.
Second, download and install Hibernate
Hibernate is a subpredal from SourceForge, searching for Hibernate from Google You can search for all connections for Hibernate. Specifically, you can get the downloaded connection from http://hibernate.sourceforge.net, the latest Release version is Hibernate 2.0.1 (2003-6-17), of course, you can also get the source code from the CVS , Then compile it by yourself.
Third, Hibernate Basic Concept
Hibernate technology is essentially a middleware that provides database services. Its architecture is shown below:
This figure shows Hibernate's working principle, which uses databases and other profiles such as hibernate.properties, XML mapping, etc. to provide data lasting services for applications. Its API contains the following major classes.
1, sessionFactory (net.sf.hibernate.SessionFactory) contains mappings, is a factory that manufactures Session, possibly contains data that can be shared between various transactions.
2, session (net.sf.hibernate.Session)
Single threaded, short life objects represent a process of a session. In fact, a JDBC Connection packaged, which can contain some caches of persistent objects.
3, Persistent Objects and Collectes
Single threaded, short life objects, including various attribute fields and some business logic, may just be some ordinary JavaBeans, but it must only be associated with a session at some point.
I will introduce these, detailed everyone can look at the documentation that Hibernate comes with it, written very detailed.
Fourth, start writing the program.
I am ready to briefly describe the powerful features of Hibernate with a better example. My program uses some other related technologies such as Ant, Log4j, Juit, etc. If you want to get these technical documents, you can contact me.
Before starting this example, I will introduce this example of this example program. I will gradually introduce Hibernate's feature. To do this, I will write a simple example, and then we make it complicated.
This, we assume that we have to write an application about children (Child), mainly manage children's related information (such as children's toys to Toy), then we must first model. as follows:
The actual class code:
Child class mainly contains an ID, and a toys property indicating that a child can have multiple TOYS, which is an array form.
Public class child {
Public int getId () {
Return ID;
}
Public void setid (int id) {
THIS.ID = ID;
}
Public void settoys (Toy [] toys) {
THIS.TOYS = TOYS;
}
Public Toy [] gettoys () {
Return TOYS;
}
Private int ID;
PRIVATE TOY [] TOYS;
}
Class Toy
Public class toy {
Public int getttoyid () {
Return Toyid;
}
Public void settoyid (int toyid) {
THIS.TOYID = TOYID;
}
Public string getname () {return name;}
Public void setname (String name) {this.name = name;}
Private int Toyid;
PRIVATE STRING NAME;
}
We can see that a child can have a lot of toys, this is a distinctive couple, there is such a label in Hibernate