Hibernate3.x: The most outstanding ORM framework

Source: Internet
Author: User
Tags generator

First, the preface

JDBC is the most common database interface in Java operation Database, which isolates the complexity of the database, so that the programmer can put the main energy into the program logic. JDBC also provides a simple way to interact with a database, such as opening a database table, executing a SQL statement, and so on. This requires a lot of code to be written for complex programs, so in recent years there have been a number of frameworks in the Java database field that have redefined JDBC to a higher level of encapsulation. such as the early Ibatis. This framework is very convenient to use. It is also not based on a complex object-oriented model. Also does not work in complex diagrams. This first-level framework isolates the database from the application. Enable programmers to manipulate only logical databases. However, because this framework is not a basic object-oriented and relational model, it is still stretched in response to large-scale applications. Based on these deficiencies, many relational mapping (ORM)-based database frameworks have recently become popular. Hibernate is one of the best. The basic idea of ORM is to abstract different databases using foreign keys and appropriate constraints. In the latest hibernate3.x, it enhances control over constraints, is more powerful and easier to use.

At the start of these ORM frameworks, many people think that the ORM framework is only one of multiple choices. Even if an ORM framework does a very good job of palladium, the mapping between the object and the database is perfect. They believe that using SQL to write programs directly is the way to go, and using auto-generated SQL is inefficient and inflexible. But as the scale of the process grows, the idea becomes increasingly untenable. This is mostly not just because using SQL directly produces a lot of code, but because using an ORM framework, we're going to operate a completely different layer: the ORM layer. Using SQL directly may also create other problems, such as the n+1 selection problem that we often encounter. And when we connect a lot of tables, we write very similar SQL statements over and over again. If we use Hibernate, the amount of these questions will vanish. We can make very simple hql to complete the above complex problem. Like Hibernate. The ORM framework should also be able to perform various optimizations to optimize the operation. For the time being, these frameworks are becoming more and more optimized, and are gradually replacing the way the database is manipulated with JDBC and SQL.

Although you can use an ORM framework to write most programs, you sometimes need to use SQL directly to manipulate them. Perhaps Hibernate's development team is aware of this, as well as providing hibernate with the ability to execute SQL directly. In earlier versions of Hibernate, the solution was to expose the JDBC connection directly to the user, so that the programmer could execute SQL directly using prepared statment. But in the new hibernate3.x, the situation has been changed. Hibernate3.x can now write an entire application without using a single SQL, and this does not affect hibernate flexibility, but it can also use all of the other features of hibernate.

Second, hibernate3.x function demo

The above said a lot of hibernate benefits, but also let us understand the hibernate3.x in this aspect of the outstanding performance. We will use a simple person-employment-oranization model to illustrate. The simplest class is person, and the following is its definition:

<class name="Person" lazy="true">
<id name="id" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="name" not-null="true"/>
<loader query-ref="person"/>
<sql-insert>INSERT INTO PERSON (NAME, ID) VALUES ( UPPER(?), ? )</sql-insert>
<sql-update>UPDATE PERSON SET NAME=UPPER(?) WHERE ID=?</sql-update>
<sql-delete>DELETE FROM PERSON WHERE ID=?</sql-delete>
</class>

Looking at the definition above, we might first notice three handwritten SQL statements: INSERT, update, and delete. Which of these will match the two properties listed above (these two properties are ID and name). In addition to this, these three statements have nothing.

Perhaps a lot of readers are most interested in <loader> tags. This tag defines a named query. This query executes at any time when we use Get (), load () loads person, or uses lazy associations to fetch data. In general, this named query should be an SQL statement, as shown here:

<sql-query name="person">
<return alias="p" class="Person" lock-mode="upgrade"/>
SELECT NAME AS {p.name}, ID AS {p.id} FROM PERSON WHERE ID=? FOR UPDATE
</sql-query>

Note: a local SQL query may return multiple entity columns, but this example is simpler and returns only one entity.

Employment is relatively more complex, and not all attributes are included in insert and update. The definition is as follows:

<class name="Employment" lazy="true">
<id name="id" unsaved-value="0">
<generator class="increment"/>
</id>
<many-to-one name="employee" not-null="true" update="false"/>
<many-to-one name="employer" not-null="true" update="false"/>
<property name="startDate" not-null="true" update="false"
insert="false"/>
<property name="endDate" insert="false"/>
<property name="regionCode" update="false"/>
<loader query-ref="employment"/>
<sql-insert>
INSERT INTO EMPLOYMENT
(EMPLOYEE, EMPLOYER, STARTDATE, REGIONCODE, ID)
VALUES (?, ?, CURRENT_DATE, UPPER(?), ?)
</sql-insert>
<sql-update>UPDATE EMPLOYMENT SET ENDDATE=? WHERE ID=?</sql-update>
<sql-delete>DELETE FROM EMPLOYMENT WHERE ID=?</sql-delete>
</class>

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.