What is Hibernate?

Source: Internet
Author: User

Hibernate class Note 1 What is hibernate

Hibernate is the operational framework for a database

1.1 The disadvantages of JDBC

L JDBC is less efficient

Each request, open connection, send SQL statement, parse SQL statement in database, etc., need to consume a lot of resources.

A lot of repetitive code

Every time we need to close Connection,statement,rs

JDBC does not do object-oriented programming

Data cache not done

Now the world's data cache is doing a pretty good frame (hibernate,oscache,ehcache)

Oscache and Ehcache both have page caches

* The data on the page is cached on the local disk

* When the application is accessed again, the data is read directly from the disk

OK

* Page cache is not suitable for updating particularly frequent data

Advantages of 1.2 JDBC

Is the framework of the lowest operating database, so at the Java level it says the fastest

JDBC did the connection pool.

1.3 Hibernate Benefits

L Object-Oriented programming

The bottom of Hibernate is also implemented by JDBC

L Hibernate's data cache is doing pretty good

L hibernate did the connection pool.

1.4 Hibernate Disadvantages

Hibernate does not allow programmers to interfere with the generation of SQL statements, and SQL statements are generated by hibernate internal operations JDBC, so in a project, if the optimization requirements for SQL are high, this time, it is not suitable for hibernate

If the amount of data in a single table is particularly large (tens), it is not suitable for hibernate

2 Learning content of Hibernate

L LEARN the crud operations of Hibernate

L PERSIST the state of an object

L one-to-many and many-to-many (emphasis)

L data cache (primary cache, level two cache, query cache) (emphasis) Characteristics of data caching

L HQL Statement

3 Hibernate CRUD Operations

3.1 Class-to-table mapping relationship

3.2 Hibernate configuration file load Mode 3.2.1 Mode one

Attention:

1. The name of the configuration file must be Hibernate.cfg.xml

2, the Hibernate.cfg.xml file must be placed under the Classpath

3.2.2 Way Two

Attention:

1. Use the name of the method configuration file to write freely

2, the path can be placed freely

3. Resource represents path name + file name

Note: Hibernate transactions are not automatically committed by default

3.3 Crud Operational Considerations 3.3.1 Session.get method

1, to use the Session.get method, then the corresponding persistence class must be guaranteed to have a non-parametric constructor

2, the second parameter of the method is consistent with the type of the primary key in the persisted class.

3. The formal parameter of the second argument of the Session.get method: Serializable. Because this is the only way to accept the type of primary key for all databases

3.3.2 Session.update

The parameter can be an object that is obtained by the Session.get method, or it can be a newly created object, but the CID value must correspond to the value of the database.

4 Hibernate process

5 Hibernate Error Analysis 5.1 error one

Analysis:

L loaded the file in the configuration file, but the path was incorrectly written and could not be found

L The file is not loaded in the configuration file at all

5.2 Error Two

Analysis:

When generating SQL statements, using the Get method to get the value, this time, the name attribute in the mapping file needs to be stitched into a get method, and then use the Java reflection mechanism to assign values, stitching error, so the property cannot be found.

5.3 Error Three

Parsing: Description Persistence not found, persistence class not found does not necessarily mean that the class does not exist, it is possible that the mapping file path is incorrectly written

The above two errors together, is summed up:

The value of the Name property of the class element is incorrectly written in the mapping file.

5.4 Error Four

The path to the mapping file is incorrectly written in Hibernate's configuration file.

5.5 Error Five

Analysis:

1, it is possible in the mapping file, the value of the class element's Name property is incorrectly written

2, when the client Session.save operation, because the operation is persisted object, so the object's class must be the name attribute of the class element in the mapping file, if not consistent, will also report such an error

5.6 Error Six

Analysis:

Session.get method Two parameters, the type of the second parameter must be consistent with the type of the primary key in the persisted class.

What is 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.