This article focuses on why you should use Hibernate, and also briefly describes how to use Hibernate, as well as some basic concepts in Hibernate. Use this article to recommend outstanding Open source ORM products to developers who have not yet contacted hibernate.
One, whyhibernate?
Now the popular "Test-driven development", similar to I think "purpose-driven learning" is a better way to accept new technologies and knowledge. Before learning the same new technology, we should first make sure that there is no need to learn, whether the existing technology has been working well, learn the new technology is to solve what problems. If you have identified the above issues, finding and learning new technologies will be more efficient and can be quickly applied to real-world development.
To say hibernate, you have to introduce Object/relationmapper (ORM), Chinese translation for object relationship mapping. The idea of such a concept stems from some of the uncoordinated thinking in the current software development. The current popular programming model is OOP (objectorientedprogramming), object-oriented programming, and the current popular database model is relationaldatabase, the two ways of thinking differently, which inevitably resulted in the development process of the uncoordinated. The ORM Framework (also known as the Persistence Layer framework) is designed to address this problem by masking the operation of the underlying database and providing an interface to the developer to manipulate the data in the database in an object-oriented manner. The current popular ORM framework has apachojb,hibernate,ibatis and so on, of course, the most perfect, the best use is Hibernate, at least I think so. Perhaps you are confused about the "persistence layer", in fact, it is simple to put the data into the database is called persistence (the data of the memory is not durable), then the structure of responsibility for this operation is called the persistence layer. You should have heard of the presentation layer, business layer, data layer, then the persistence layer is a layer between the business layer and the data layer, or the persistence layer is part of the data layer.
Next, I want to illustrate the benefits that ORM brings to us through an actual development example. First of all, we need a database of three tables, a student, a course, and a course_slection. Student is used to hold student information, course is used to represent course information, course_selection is used to represent the student's elective information. (The detailed structure of the table here I omit, because it is not important) a program is now required to select the name of the course selected by the student, and there may be several ways in which the following programs can be written:
1. Rookie Grade
Code Fragment 1:
Publiclistselectcourses (Stringstudentid)
{
Connectioncon=null;
Statementsta=null;
Try
{
Class.forName ("Oracle.jdbc.driver.OracleDriver");
Con=drivermanager.getconnection (
"Jdbc:oracle:thin:@10.85.33.199:1521:glee",
"Test", "test");
Stringsql= "Select*fromcourse_selection";
stringsql2= "Selectnamefromcoursewhereid= '";
Sta=con.createstatement ();
Resultsetrs=sta.executequery (SQL);
Listlist=newlinkedlist ();
while (Rs.next ())
{
Resultsetrs2=sta.executequery (sql2+
Rs.getstring ("course_id") + "'");
if (Rs2.next ())
{
List.add (rs2.getstring ("name"));
}
}
returnlist;
}
catch (Exceptione)
{
E.printstacktrace ();
}
Returnnull;
}
This program you must see very dizzy bar, what the mess is all together, then look at an improved program.
2. Improved code
Code Fragment 2:
Classdbhelper
{
Publicstaticconnectiongetconnection ()
{
Try
{
Class.forName (Constants.db_driver);
Returndrivermanager.getconnection (Constants.db_url,
CONSTANTS.DB_USER,CONSTANTS.DB_PWD);
}
catch (Exceptione)
{
E.printstacktrace ();
}
Returnnull;
}
}
Publiclistselectcourses (Stringstudentid)
{
Connectioncon=null;
Statementsta=null;
Try
{
Con=dbhelper.getconnection ();
Stringsql= "Select*fromcourse_selection";
stringsql2= "Selectnamefromcoursewhereid= '";
Sta=con.createstatement ();
Resultsetrs=sta.executequery (SQL);
Listlist=newlinkedlist ();
while (Rs.next ())
{
Resultsetrs2=sta.executequery (sql2+rs.getstring ("course_id") + "'");
if (Rs2.next ())
{
List.add (rs2.getstring ("name"));
}
}
returnlist;
}
catch (Exceptione)
{
E.printstacktrace ();
}
Returnnull;
}
The form of this code is a widely used form, relative to the first paragraph of code, should have made progress, separated the database connection operation, and the database connection information to a separate class complete (generally placed in the configuration file), Often in the development also will introduce the database connection pool (ConnectionPool) to improve the performance, I here all try to simplify. But these do not fundamentally improve the structure of the program, in the business code still mixed a lot of database operations, structure is not clear. Let's take a look at a complete separation of the database operation code:
3.DAO mode
Code Fragment 3:
Publiclistselectcourses (Stringstudentid)
{
Studentdaosd=newstudentdao ();
Studentstudent=sd.findbyid (StudentID);
Setset=student.getcourseselections ();
Listcoursenames=newlinkedlist ();
For (Iteratoriter=set.iterator (); Iter.hasnext ();)
{
Courseselectionelement= (courseselection) iter.next ();
Coursenames.add (Element.getcourse (). GetName ());
}
Returncoursenames;
}
Do you feel a lot less code? Perhaps you are a bit confused about this code, it doesn't matter, the later text will explain in detail. I'd like to explain DAO first. In fact DAO and hibernate are not necessarily linked, but generally use the Hibernate program with DAO mode. DAO's full name is Dataaccessobject, the program to access the data in the database (including acquisition, update, delete) are accessed through the DAO, in fact DAO is really masking all the database operations, so in the business code can completely isolate the data layer code. If I told you that in the real Hibernate development, to complete the functions mentioned above, the need for handwritten code is "code fragment 3" So much, or even less, you have a lot of motivation to learn Hibernate? So well, let's start the hibernate journey.