Something hibernate have to know.

Source: Internet
Author: User

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.
Session.close ();//Free State
History and background

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 a good 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.

Introduction to Hibernate Hibernate is an JDO tool. It works by creating a mapping relationship between a value object and a database table through a file (typically two types: XML files and properties files). In this way, we can achieve the purpose of using a database by manipulating these value objects and some of the basic classes provided by Hibernate. For example, using a hibernate query, you can return a list of a value object directly, without having to load the data of the result set into a single value object in the same way as traditional JDBC access, saving a lot of time for coding. The HQL provided by Hibernate is a class-SQL language that provides an object-oriented database query like EJBQL, but HQL is very close to standard SQL in terms of functionality and usage.

Second, the difference between hibernate and JDBC the main differences between Hibernate and JDBC are as follows:

1, Hibernate is a lightweight object encapsulation of JDBC, it is an independent object persistence layer framework, and app Server, and EJB have no inevitable connection. Hibernate can be used in any situation where JDBC can be used and, in a sense, hibernate to replace JDBC in any situation.

2, Hibernate is a framework closely related to JDBC, so hibernate compatibility and JDBC driver, and database have a certain relationship, but with its Java program, and app server has no relationship, there is no compatibility issues.

3. Hibernate is a substitute for JDBC and cannot be used to compare directly with entity beans.

Three, Hibernate to carry on the Multiple Table association query Hibernate query to multiple tables, the query result is the Cartesian product of multiple tables, or "cross" connection. For example: From Student, book from Student as Stu, book as Boo to Student Stu, book Boo Note: Let the Student and book in the query be the class name corresponding to the table Student and the book. Its name must be the same as the name of the class, including the case of letters. It is a good practice for aliases to be subject to the first-letter lowercase rules, which is consistent with Java's naming conventions for local variables.

There are several existing strategies for managing hiberante mapping files, such as:

· All hand-written
· Put the xdoclet tag in your Java class and let it generate the corresponding mapping file.
· Generates hibernate mapping files and Java classes from SQL schema (schema).
· The hibernate mapping file is written manually, and Java classes and SQL schemas are generated from the hibernate mapping.
· Based on the given SQL schema, the hibernate mapping file is written manually and the Java class is generated using the Hbm2java tool.

In this article, we will look at the last of these methods. Although such choices are usually just a rough shot, this approach does have many advantages in most cases:

· Hibernate mappings are concentrated in the mapping file, rather than spreading the information in Java source code, which makes maintenance easier. In some cases, XDoclet annotation does not support all the features that are available in the Hibernate mapping pattern, and you can get better control by mapping.

· The database schema can be maintained separately, rather than from a Java class or hibernate mapping file. This allows database administrators (DBAs) who don't know much about java/hibernate to have better control over the details of the database (indexes, tablespaces, table types, etc.) themselves.

Http://java.chinaitlab.com/Hibernate/357575.html

on the characteristics and thinking of Hibernate programming

Http://java.chinaitlab.com/Hibernate/39228.html

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.