Hibernate Best Practices

Source: Internet
Author: User
1 Overview

Many Java engineers may know that Hibernate is the best and most widely used or mapping framework available today. The frame is clear, the operation is convenient, the performance is good.

However, little is known about how the framework is implemented internally. At the same time for application development, many will also involve a lot of technical requirements, such as:

• Cache the table data when the service is started;

L name Art;

L configuration file loading and parsing;

L Unique key generation;

l Use of Excellent library

L Exception architecture design and so on.

How we do it, or how to achieve it more clearly, or how to do it faster. We build cars from the ground up, or learn from successful experiences. This is a public question that lies before us.

Hibernate is for us to achieve some technical requirements, to provide a lot of technical solutions. And these technical solutions are contained in its code. Involves public library, design pattern, code structure, naming art.

Through my development experience, analysis of the development framework of the source code experience, I personally think that should not be built from scratch, even if the design model, to write code, rather than find some success stories to design the relative technical requirements, but also from the beginning to build a car. I recommend that for technical requirements, find successful implementations that may include: other departments ' design approach, open source framework, colleague experience, etc. Combine their own systems, learn from successful cases, frameworks and so on. , to better and faster to meet the needs of rapid development of business.

This article on the framework for Hibernate, through my framework code reading and analysis, the following some of the content is organized. Including:

L Design details and naming art

L Best Implementation

L Implementation Mechanism

L Graceful Code

L HQL Instances

L References

Reading, analyzing, applying, summarizing, and publishing documents are the only way to become an excellent engineer or architect for good open-source framework code or good code for internal or other teams.

Quote a WORD from a Java guru. Question: Is there a nootropic training or fun behavior that makes you feel like you can be a better developer? Josh Bloch: I think math and writing can make you a better developer. Mathematics, like programming, requires rigorous thinking. And writing will force you to organize your thoughts. Mathematics and writing all train the same aesthetic function, which is also necessary to write good procedures.

2 design details and naming art 2.1 User-provided method-usersuppliedconnectionprovider

/**

* Implementation of the <literal>ConnectionProvider</literal> interface that

* Simply throws an exception if a connection is requested. This implementation

* Indicates that the user was expected to supply a JDBC connection.

* @see ConnectionProvider

* @author Gavin King

*/

public class Usersuppliedconnectionprovider implements ConnectionProvider { 2.2 connection Provider factory class-factory

Connectionproviderfactory

Create method: New Start

/**

* Instantiates a connection provider given either <tt>System</tt> properties or

* a <tt>java.util.Properties</tt> instance. The <tt>ConnectionProviderFactory</tt>

* First attempts to find a name of a <tt>ConnectionProvider</tt> subclass in the

* Property <tt>hibernate.connection.provider_class</tt>. If missing, heuristics is used

* to choose either <tt>drivermanagerconnectionprovider</tt>

* <TT>DATASOURCECONNECTIONPROVIDER</TT>, <tt>C3P0ConnectionProvider</tt> or

* <TT>DBCPCONNECTIONPROVIDER</TT>.

* @see ConnectionProvider

* @author Gavin King

*/

2.3 Verifying-verify

Environment

Verifyproperties (global_properties);

L 2.4 Verify that the class supports-supports

boolean linkedhashsupport;

Try {

Class.forName ("Java.util.LinkedHashSet");

Linkedhashsupport = true;

}

Catch (ClassNotFoundException Cnfe) {

Linkedhashsupport = false;

} 2.5 Delegate-delegate

Purpose: Make the function class code clearer and easier to maintain and extend.

Methods: You can delegate the implementation of some functionality internal implementations to the help class.

/**

* The helper instance which contains much of the code which we

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.