Hibernate environment Construction Course---"Small white series" 0 basic to Skilled application Hibernate framework (i) _hibernate

Source: Internet
Author: User

Tutorial vulgar violence, with their own understanding of the elaboration of hibernate, suitable for small white, great God bypass.

Also, it would be easier to understand if you have javaweb servlet programming experience.

Not much nonsense, straight into the Hibernate framework Overview first, the concept of Popular Science framework: The framework is to encapsulate a large number of functions to call us, such as utlis.getxxx (); This is convenient for our program Wang not to develop themselves, improve the efficiency of development. Let's take a look at the previous servlet programming process

Diagram: This is the way we used to write programs, Web-->service-->dao where the DAO layer we use JDBC technology, with dbutils to connect and manipulate the relational database that our hibernate is replacing the previous JDBC Dbutils tool to implement the operation of relational database so: Hibernate is a framework of DAO layer, Hibernate function is to replace the previous Jdbc|dbutils operation database. The difference between the Javaweb base stage and the hibernate operation let's take a comparison and see why we have to learn this framework. Dbutils used to be very cool, too.


We come to a business that illustrates the above two graphs, the front-end form forms registered users, including 20 user attributes (name Age hobby ...). And so on), the servlet uses Beanutil to encapsulate the user object and pass it to DAO, and we want to write

Object [] parms {User.getid,user.getpassword};

Assign all 20 fields to an array. The INSERT statement is then executed. This is the first picture of the operation, the second picture to hibernate to operate after it? We call directly
Session.save (user); This allows the entire user object to be saved to the database, greatly simplifying the operation.
Heard here, the little friends are not very excited. Please look down.
ORM Idea
ORM (object relation Mapping) translation is the Object Relational mapping hibernate is the use of this idea, the operation of the database into object-oriented.

Diagram: Object that is the user object, we pass the user object hibernate in the DAO layer, hibernate the table relationship that the user object maps to a database that's the relational database.
Well, if you have questions about this picture, buddy, look down and talk about how the user object maps to a database table, and it's a little bit clearer.

Build Hibernate
Guide Bag:
1.hibernate framework must depend on the package

2. Import MySQL
3.C3P0 (optional)

And right-click Build path add to..
4. Let's prepare the database.

The table statement is as follows
CREATE TABLE ' Cst_customer ' (
' cust_id ' bigint not NULL auto_increment COMMENT ' customer number (primary key) ',
' cust_name ' varchar not NULL COMMENT ' customer name (company name) ',
' cust_mobile ' varchar DEFAULT NULL COMMENT ' mobile phone ',
PRIMARY KEY (' cust_id ')
) Engine=innodb auto_increment=1 DEFAULT Charset=utf8;

To create a Java entity from a table structure

Look at the annotations and generate them yourself. Don't forget..
Creating a mapping File

You need to put the mapping file in the same level as your entity, and name the entity Bean +hbm.xml
Once created, we open the XML file and start the configuration.
1. Introducing Constraints

<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
2. Save the file and close it.
3. Open again, enter a < see if you have any prompts.


Close and open without prompting ... Until the prompt appears, PS: Because just introduced, a little temper normal. )
4. Configuration is as follows, the explanation is also in the code inside

Hibernate master configuration file (built in src folder, name Hibernate.cfg.xml)
Introducing constraints
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >

Let's just get these two tabs out of the way.
The main configuration file is divided into three main steps
1. Necessary configuration (must match. Brother. )

2. Optional configuration


3. Introducing the mapping file


At this end, our hibernate is the import complete, please see the next blog, we continue to learn the API in Hibernate, as well as the database additions and deletions to check the operation


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.