From head to heels, you explain what hibernate is.

Source: Internet
Author: User
Tags modify sql object model version
Hibernate is a free open source Java package that makes it easy to deal with relational databases just as your database contains ordinary Java objects you use every day, without having to think about how to take them out of a cryptic database table (or back into a database table). It frees you up to focus on the objects and features of your application without worrying about how to save them or how to find them later.
  
Most applications need to process data. When Java applications run, they tend to encapsulate data as interconnected object networks, but when the program ends, the objects disappear into a cluster of logic, so you need to have some way to save them. Sometimes, even before the application is written, the data already exists, so you need to have a way to read them and represent them as objects. Writing code manually to perform these tasks is tedious, error-prone, and consumes a large part of the development effort of the entire application.
  
Good object-oriented developers are tired of this repetitive work, and they start using the usual "positive" lazy approach, that is, creating tools that automate the process. For relational databases, the greatest achievement of this effort is the object/relational mapping (ORM) tool.
  
There are many such tools, from expensive commercial products to the EJB standard that is placed in Java EE. However, in many cases, these tools have their own complexities, making it necessary for developers to learn the detailed rules for using them and modify the classes that make up the application to meet the needs of the mapping system. As these tools evolve to cope with more stringent and complex enterprise requirements, the complexity of using them in simpler and more common scenarios is overshadowed by the benefits that can be gained. This has led to a revolution that has facilitated the emergence of lightweight solutions, and hibernate is one such example.
  
   How the Hibernate works
  
Hibernate will not hinder you and will not force you to modify the way objects behave. They do not need to implement any magical interfaces in order to be able to persist. The only thing you need to do is create an XML "map document" that tells hibernate the classes you want to be able to save in the database and how they relate to the tables and columns in that database, and then you can ask it to get the data as an object, or to save the object as data. It's almost perfect compared to other solutions.
  
Since this article is an introductory article, it will not introduce concrete examples of building and using Hibernate mapping documents (an example I've already covered in the first chapters of the book Hibernate:a Developer's Notebook). In addition, some good examples can be found in online and hibernate documentation, see the "More Information" section below. It's actually quite intuitive. Properties in an Application object are associated with the correct database structure in a simple and natural way.
  
At run time, hibernate reads the mapping document and then dynamically builds Java classes to manage the conversion between the database and Java. In Hibernate, there is a simple and intuitive API for executing queries on objects represented by the database. To modify these objects, (in general) simply interact with them in the program, and then tell Hibernate to save the changes. Similarly, creating new objects is simple, you simply create them in a normal way, and then tell hibernate about them, so you can save them in the database.
  
The Hibernate API is easy to learn, and its interaction with the program flow is quite natural. Call it at the right place, and you can achieve your purpose. It brings a lot of automation and code savings benefits, so it's worthwhile to spend a little time learning it. Another benefit is that the code does not care about the type of database to use (or even must know). My company has had the experience of being forced to replace a database vendor later in the development process. This can be a huge disaster, but with the help of Hibernate, simply modify the hibernate configuration file.
  
The discussion here assumes that you have created a relational database by creating a Hibernate mapping document, and that you have a Java class to map. There is a hibernate toolset that can be used at compile time to support different workflows. For example, if you already have Java classes and mapping documents, hibernate can create (or update) the necessary database tables for you. Alternatively, hibernate can also generate data classes just starting with the mapping document. Alternatively, it can reverse design your database and classes to map the document. There are also alpha Plug-ins for Eclipse, which provide intelligent editing support and graphical access to these tools in the IDE.
  
If you are using a Hibernate 2 environment, these tools are rarely available, but there are third-party tools available.
  
   the occasion of using Hibernate
  
Why use other tools, since hibernate looks so flexible and useful? Here are some scenarios that can help you make judgments (perhaps by providing comparisons and contexts that can help identify situations that are very suitable for hibernate).
  
If the application is simple for data storage-for example, you just want to manage a set of user preferences-you don't need a database at all, let alone an excellent object-relational mapping system (even if it's as easy to use as Hibernate)! Starting with Java 1.4, there is a standard Java Preferences API that works well. (More information about the preferences API can be found in the Onjava article.) )
  
For people familiar with using relational databases and understanding how to perform perfect SQL queries with enterprise databases, Hibernate seems to be a bit of a drag, like a speedboat with power and automatic gears that makes performance-oriented racing drivers impatient. If you belong to this person, if your project team has a strong DBA, or if there are some stored procedures to process, you may want to study ibatis. The founders of Hibernate themselves regard Ibatis as another interesting option. I am interested in it because we have developed a similar system for an E-commerce site (which is more powerful) and since then we have used it in other environments, although we usually prefer to use hibernate in new projects after discovering hibernate. You can assume that a SQL-centric solution (such as Ibatis) is a "reverse" object/relational mapping tool, while Hibernate is a more traditional ORM.
  
Of course, there are other external causes that can lead to alternative methods. For example, in an enterprise environment, you must use a mature EJB architecture (or some other generic object mapping system). You can tailor code to the platform that provides your own data storage tools, such as Mac OS X's Core data. It is possible to use a storage specification like an XML DTD, which 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 you are about to start or have decided to use a relational database, as long as it 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 a choice), then hibernate is probably your ideal choice. You may be amazed at the amount of time you save and how much you will like to use it.
  
   Other Information
  
The Hibernate project has a large number of online documents that can help you find your way and start using it quickly.
  
The authoritative reference is the hibernate in Action, the author is Christian Bauer and Gavin King, all of whom are Hibernate's founders. The book is a comprehensive and basic account of the functions of the hibernate package and the correct way to use it.
  
Reading my book hibernate:a Developer ' s notebook is also a good way to start quickly. It directly but in detail describes how to set up hibernate in a Java project and how to use some of its most important features. 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 version of both software. In any case, the basic concept is correct, and I hope to be able to update the book for Hibernate 3 as soon as possible.
  
Another interesting book is better faster lighter Java, the author of Bruce Tate and Justin Gehtland. The book gives some practical ways to accomplish actual projects in a reasonable way, which is one of the reasons for its popularity. It gives reasonable advice on how to evaluate and use (or veto) available Java technologies, and mentions Hibernate and spring as examples of the correct approach.
  
Finally, "Working with Hibernate in Eclipse", which mentions the more powerful new Alpha version of the Hibernate 3 tool, describes in detail how to call a Hibernate Synchronizer's eclipse plug-in is used with hibernate.

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.