Hibernate (2) Simple implementation and comparison of hibernate principles

Source: Internet
Author: User

Hibernate (2) Simple implementation and comparison of hibernate principles

As an implementation of the orm model, hibernate serves as a bridge between the java object model and the relational model, mainly through jdbc


To achieve the purpose of operating the database, provides a relatively comprehensive set of automated APIs. Simulate


The operating principle is mainly the use of jdbc. Let's take a look at this small example.

Package com. tgb. cfl. hibernate; import java. lang. reflect. method; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. SQLException; import java. util. hashMap; import java. util. list; import java. util. map;/*** simulate the hibernatesession interface * @ author Chen fanglin ***/public class Session {// stores the object attribute private Map
 
  
Columns = new HashMap
  
   
(); // String Array stores the object's get method set String methodNames []; // initializes the object class attributes and the method set public Session () {// initializes the object, you do not need to read the configuration file. Columns. put ("name", "name"); columns. put ("age", "age"); methodNames = new String [columns. size ()];}/*** save method, persistence object * @ param user */public void save (Person person) {// strColumn represents the attribute column in the database table. And connect them. String strColumn = ""; int index = 0; for (String key: columns. keySet () {strColumn + = key + ","; String v = columns. get (key); // get method for obtaining attributes v = "get" + Character. toUpperCase (v. charAt (0) + v. substring (1); methodNames [index] = v; index ++;} strColumn = strColumn. substring (0, strColumn. length ()-1); // The placeholder of the concatenation parameter, that is :(?, ?) String strValue = ""; for (int I = 0; I
   
    

This example is mainly used to simulate the operations on the object class in the session interface, mainly divided into the following steps:

1. dynamically concatenate SQL statements

2. get the object class attribute Operation Method Based on reflection ***

3. Obtain the database connection

4. Update data through the PreparedStatement object

This is a jdbc object, and the steps are mainly divided

A) obtain the PreparedStatement object from the connection.

B) Use the PreparedStatement object and get method to get the attribute value.

5. Garbage Collection (recycle connection, object closed)

Let's look back at hibernate through this example. What does hibernate do for us?

Step 1: How does one implement dynamic concatenation of SQL statements? In hibernate, a configuration file named ***. hbm. xml is used to configure the relationship between entities and databases. This is also the Protocol agreed by hibernate. If you write it like this, it will eventually read the file with such a suffix. This is the first step.

Step 2: Use reflection to get the corresponding get method. This is done using reflection, mainly through the internal jdk.

Java. util. reflect. method class. It is also based on jdk APIs.

Step 3: Obtain the database connection. In hibernate, the configuration file is called the hibernate. properties configuration file. The database connection dialect, database driver, database address, user name and password are configured internally.

For example:

This is also stipulated by hibernate. Well, if you have this file in the specified folder in your program, I can directly read the node data. However, the above example is written in the code.

Step 4: This is the api of the jdbc preparedstatement object

Step 5: correspond to the jdk api.

The standard hibernate steps are as follows:

1. Read and parse the hibernate. cfg. xml Configuration file through Configuration (). configure ();

2. Read hibernate. cfg. xml // read and parse the ing information

3. Create SessionFactory through config. buildSessionFactory (); //

4. sessionFactory. openSession (); // open Sesssion

5. session. beginTransaction (); // create transaction Transation

6. persistent operate persistent operation

7. session. getTransaction (). commit (); // submit the transaction

8. Disable Session and SesstionFactory

Through this figure, we find that hibernate adds an additional layer to JDBC. It reminds me of where I heard a sentence and flexible configuration.

What is it? It is nothing more than adding a layer, which is quite pleasant. Later I thought it was just a bit more reasonable.


So let's look back. In fact, in general, hibernate is only encapsulated in jdbc, which simplifies the direct operation of jdbc.

. In java, jdbc is the most primitive and direct method. It cannot be said that this method is not good because

Ibatis is also used to create different SQL statements for entities. Therefore, the original method also has its advantages.

For example, flexible and efficient. In other words, hibernate is encapsulated on the basis of jdbc, which also avoids programmers.

To directly write tedious SQL statements, as well as transaction maintenance and Cache control. In addition, open source is also an aspect.

Of course, here the explanation of the hibernate principle is only the tip of the iceberg of hibernate. Later, such as hibernate cache, three States, processing of object associations, and transactions, all need to be well studied.

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.