What is hibernate?

Source: Internet
Author: User

Hibernate
Hibernate is a free open-source Java package that makes it easy to deal with relational databases, just as your database contains common Java objects used every day, at the same time, you do not have to consider how to extract them from the mysterious database table (or put them back to the database table ). It frees you from allowing you to focus on the objects and functions of your applications without worrying about how to save them or how to find them later.

This article discusses the following:

  • History and background
  • How hibernate works
  • Use hibernate
  • Other information

History and background
Most applications need to process data. When Java applications run, data is often encapsulated into a network of interconnected objects. However, when the program ends, these objects will disappear into a group of logic, therefore, you need some methods to save them. Sometimes, even before writing an application, the data already exists, so you need to read them and present them as objects. Writing code manually to execute these tasks is not only tedious and error-prone, but also takes up a large part of the development workload of the entire application.

Good object-oriented developers get tired of repetitive work and begin to adopt the usual "active" lazy approach, namely, creating tools to automate the entire process. For relational databases, the biggest achievement of such efforts is the object/relational ing (ORM) tool.

There are many such tools, from expensive commercial products to the EJB standards built into J2EE. However, in many cases, these tools have their own complexity, so developers must learn to use their detailed rules and modify the classes that constitute the application to meet the needs of the ing system. As these tools continue to develop to cope with more stringent and complex enterprise needs, the complexity of using them in relatively simple and common scenarios exceeds the benefits they can achieve. This caused a revolution and promoted the emergence of lightweight solutions. Hibernate is such an example.

How hibernate works
Hibernate does not impede you or force you to modify the behavior of objects. They do not need to implement any incredible interfaces to survive. The only thing that needs to be done is to create an XML "ing document" That tells hibernate that you want to save classes in the database and how they are associated with tables and columns in the database, then it can be required to obtain data in the form of an object or save the object as data. Compared with other solutions, it is almost perfect.

Since this article is only an introductory article, we will not introduce a specific example of building and using the hibernate ing document (in hibernate: an example is provided in the first chapters of a developer's notebook ). In addition, you can find some good examples in the online and hibernate documents. For more information, see the "Other Information" section below. It is actually quite intuitive. Attributes in an application object are associated with the correct database structure in a simple and natural way.

During runtime, Hibernate reads the ing document and dynamically constructs Java classes to manage the conversion between the database and Java. In hibernate, there is a simple and intuitive API used to query the Objects represented by the database. To modify these objects, you only need to interact with them in the program, and then tell hibernate to save the modification. Similarly, it is easy to create new objects. You only need to create them in the conventional way and then tell hibernate about them so that they can be saved in the database.

The Hibernate API is easy to learn, and its interaction with the Program Stream is quite natural. You can call it at an appropriate location to achieve your goal. It brings a lot of advantages in automation and code saving, so it is worth a little time to learn about it. In addition, you can also obtain another benefit, that is, the Code does not need to care about the type of database to be used (otherwise, you must even know ). My company once experienced the forced replacement of the Data Warehouse vendor in the later stages of the development process. This will cause a huge disaster. However, with hibernate, you only need to modify the hibernate configuration file.

The discussion here assumes that you have created a relational database by creating the hibernate ing document and have the Java class to be mapped. There is a hibernate "tool set" that can be used during compilation to support different workflows. For example, if you already have a Java class and ing document, Hibernate can create (or update) required database tables for you. Alternatively, Hibernate can also generate data classes only starting from the ing document. Alternatively, you can design your database and classes in reverse order to develop a ing document. There are also Alpha plug-ins for Eclipse, which can provide intelligent editing support in IDE and graphic access to these tools.

If you are using the hibernate 2 environment, these tools are rarely provided, but there are available third-party tools.

Use hibernate
Since hibernate looks so flexible and easy to use, why other tools? The following are some scenarios that can help you make judgments (perhaps by providing some comparisons and context, you can help identify scenarios that are very suitable for hibernate ).

If the application needs data storage very easily-for example, you only want to manage a group of users first-you do not need a database, not to mention a good object-link ing System (even if it is as easy to use as Hibernate )! Since Java 1.4, a standard Java Preferences API can play this role well. (You can find more information about the preferences API in the onjava article .)

For those who are familiar with relational databases and know how to execute perfect SQL queries to interact with enterprise databases, Hibernate seems to be a little out of the way, this is just like a speedboat with power and automatic gear will make performance-oriented drivers impatient. If you belong to this type of person, if your project team has a strong dBA, or you have some stored procedures to handle, you may want to study ibatis. The creator of hibernate regards ibatis as another interesting choice. I am very interested in it, because we once developed a similar system for an e-commerce site (with more powerful functions), and from that time until now, we have already used it in other environments, although we prefer hibernate in new projects after we discover hibernate. You can think that the SQL-centered solution (such as ibatis) is a "reverse" Object/link ing tool, while Hibernate is a more traditional Orm.

Of course, there are other external reasons that may lead to another method. For example, in an enterprise environment, you must use a mature EJB architecture (or some other non-Common Object ing systems ). Code tailored to platforms that provide their own data storage tools, such as Mac OS X's core data. A storage specification such as an xml dtd does not involve relational databases at all.

However, if you are using a rich object model and want to save it flexibly, easily, and efficiently (whether or not you are about to start or have decided to use relational databases, as long as this is an option-and there are excellent free databases available, such as MySQL, or HSQLDB that can be embedded in Java, it should always be an option ), hibernate is probably your ideal choice. You may be surprised to save much time and enjoy it.

Other information
The Hibernate project has a large number of online documents to help you identify the direction and start using it quickly.

The authoritative reference material is hibernate in action. The authors are Christian Bauer and Gavin king, both of which are the creators of hibernate. This book provides a comprehensive and basic description of the functions and correct usage of the hibernate package.

Reading my book hibernate: A developer's notebook is also a good way to get started quickly. It provides a direct but detailed description of how to set up hibernate in a Java project and how to use some of its most important functions. The code examples are generally based on earlier versions of Hibernate and HSQLDB, so if you want to use them without modification, you need to use the correct versions of the two software. In any case, the basic concepts are correct, and I hope to update the book for hibernate 3 as soon as possible.

Another interesting book is better faster lighter Java, written by Bruce Tate and Justin gehtland. The book provides some practical methods to complete the actual project in a reasonable way, which is also one of the reasons for its popularity. It provides reasonable suggestions on how to evaluate and use (or reject) the available Java technology, and mentions Hibernate and spring as examples of the correct method.

Finally, "working with hibernate in Eclipse" (it comes up with a more powerful new alpha version of The Hibernate 3 tool) it details how to use an Eclipse plug-in called hibernate Synchronizer with hibernate.

Source:
What's hibernate http://www.onjava.com/pub/a/onjava/2005/09/21/what-is-hibernate.html

Author Profile
  A Senior Software Engineer of berbee, with more than 15 years of experience as a system developer. Hibernate: the author of a developer's notebook.

 
Note: I am not liable for any of the above content from the Internet.

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.