Hibernate (Open source Object Relational mapping framework)

Source: Internet
Author: User

Hibernate is an open source object-relational mapping framework that provides JDBC with a very lightweight object encapsulation that maps Pojo to database tables, is a fully automated ORM framework, and Hibernate automatically generates SQL statements, automatically executes, So that Java programmers can use object programming thinking to manipulate the database at will. Hibernate can be applied to any JDBC application, both in Java client applications and in servlet/jsp Web applications, and most revolutionary of all, hibernate can replace CMP in the EE architecture of the EJB application. The task of achieving data persistence.

Core interfaces and classes

There are 6 core classes and interfaces for hibernate: Session, Sessionfactory, Transaction, Query, criteria, and configuration. These 6 core classes and interfaces are used in any development. Through these interfaces, you can not only access persistent objects, but also enable transaction control. These 6 core interfaces and classes are described separately below.SessionThe session interface is responsible for performing crud operations on persisted objects (the task of crud is to complete the communication with the database, including many common SQL statements). However, it is important to note that the session object is non-thread-safe. At the same time, Hibernate's session differs from the HttpSession in JSP applications. When the term "session" is used, it is actually referring to the session in Hibernate, and later the HttpSession object is called the user session.hibernatesessionfactoryThe Sessionfactory interface is responsible for initializing hibernate. It acts as a proxy for the data storage source and is responsible for creating session objects. The factory model is used here. It is important to note that Sessionfactory is not lightweight, because in general, a project typically requires only one sessionfactory, and when you need to manipulate multiple databases, you can specify a sessionfactory for each database.hibernatetransactionThe Transaction interface is an optional API that allows you to opt out of using this interface instead of the underlying transaction code written by Hibernate's designers themselves. The Transaction interface is an abstraction of the actual transaction implementation, which includes JDBC transactions, usertransaction in JTA, and even CORBA transactions. This design is designed to enable developers to use a single transactional interface, allowing their projects to be easily ported between different environments and containers.HibernatequeryThe query interface makes it easy to make queries against databases and persistent objects, which can be expressed in two ways: the HQL language or the SQL statement for the local database. Queries are often used to bind query parameters, limit the number of query records, and ultimately perform query operations.HibernatecriteriaThe criteria interface is very similar to the query interface, allowing you to create and execute object-oriented, standardized queries. It is important to note that the criteria interface is also lightweight and cannot be used outside the session.hibernateconfigurationThe purpose of the configuration class is to configure Hibernate and start it. During Hibernate startup, an instance of the configuration class first locates the location of the mapped document, reads the configuration, and then creates a Sessionfactory object.         Although the configuration class plays only a small role throughout the Hibernate project, it is the first object encountered when starting hibernate. SSH Framework an integration of Ssh for Struts+spring+hibernate Frame , is a popular open source framework for Web applications at present. The system of the integrated SSH framework is divided into four layers: presentation layer, business logic layer, data persistence layer and Domain module layer to help developers build clear, reusable and easy-to-maintain web applications in a short period of time. It uses struts as the overall infrastructure of the system, is responsible for the separation of MVC, controls the business jump in the model part of the Struts framework, supports the persistence layer with the Hibernate framework, manages the spring, manages struts and hibernate. By using object-oriented analysis method, we propose some models, implement these models as basic Java objects, and then write the basic DAO (Data Access Objects) interface, and give the DAO implementation of Hibernate. The DAO class implemented by Hibernate architecture realizes the transformation and access between Java class and database, and finally the management of spring, and the basic business process of managing struts and Hibernate system is: In the presentation layer, the interactive interface is first implemented through JSP page. is responsible for receiving requests and transmitting responses (Response), and struts delegates the request received from Actionservlet to the corresponding action processing according to the configuration file (Struts-config.xml). In the business layer, the spring IOC container that manages the serviced component is responsible for delivering the business model component to the action and the component's collaboration Object data Processing (DAO) component to complete the business logic and to provide container components such as transaction processing, buffer pooling to improve system performance and ensure data integrity. In the persistence layer, it relies on Hibernate's object mapping and database interaction, processing the data requested by the DAO component, and returning the processing results.Frame StructureSSH Framework Struts1     struts provides the corresponding components for both Model,view and controller.      Actionservlet, this class is the core controller of STRUTS1 and is responsible for intercepting requests from users.      action, which is typically provided by the user, is responsible for receiving requests from Actionservlet and invoking the model's business logic method to process requests based on the request, and returns the processing results to the JSP page display.       Model Part:     consists of actionform and JavaBean, Where Actionform is used to encapsulate the user's request parameters into an Actionform object, which is Actionservlet forwarded to Action,action to process the user's request based on the request parameters inside the Actionform.      JavaBean encapsulates the underlying business logic, including database access.      View Section:    This part uses JSP (or HTML, PHP ...). Achieve     struts provides a rich library of tags that can reduce the use of scripts through a tag library, and a custom tag library enables effective interaction with the model and adds real-world functionality. The corresponding JSP part. The      controller component:    Controller component consists of two parts-system core controller, business logic controller.    system core Controller, corresponding to the upper Actionservlet. The controller inherits the HttpServlet class, so it can be configured as a callout servlet. The controller is responsible for intercepting all HTTP requests and then deciding whether to transfer to the business logic controller based on user requests. The     business logic controller, which handles user requests, does not have the processing power, but calls model to complete processing. Corresponds to the action section.    SSH framework Spring      Spring is an open source framework that was created by Rod Johnson. It is created to address the complexities of enterprise application development. Spring uses basic JavaBean to accomplish things that were previously only possible by EJBS. However, the use of spring is not limited to server-side development. From the standpoint of simplicity, testability, and loose coupling, any Java application can benefit from spring.       Objective: To solve the complexity      function of enterprise application development: Use basic JavaBean instead of EJB, and provide more enterprise application function       Scope: Any Java application      Simply put, Spring is a lightweight control inversion (IoC) and facet-oriented (AOP) container framework.      Lightweight-Spring is lightweight both in terms of size and overhead. The full spring framework can be published in a jar file with a size of more than 1MB. and the processing overhead required by spring is negligible. In addition, spring is non-intrusive: Typically, objects in spring applications do not depend on a particular class of spring. Control reversal--spring facilitates loose coupling through a technique called inversion of Control (IoC). When an IOC is applied, other objects that an object relies on are passed in passively, rather than the object itself creating or locating dependent objects. You can think of the IOC as opposed to Jndi--not the object looking for dependencies from the container, but the container actively passing the dependency to it when the object is initialized without waiting for the object to be requested. Facet-oriented--spring provides rich support for aspect-oriented programming, allowing for the development of cohesion through the separation of application business logic with system-level services such as auditing (auditing) and transaction (transaction) management. The Application object only implements what they should do-complete the business logic-that's all. They are not responsible (or even conscious) for other system-level concerns, such as log or transaction support.       Container--spring contains and manages the configuration and lifecycle of the Application object, in this sense it is a container where you can configure how each of your beans is created-based on a configurable prototype (prototype), Your bean can create a separate instance or generate a new instance each time it is needed-and how they relate to each other. However, Spring does notShould be confused with traditional heavyweight ejb containers, which are often bulky and cumbersome and difficult to use. The       framework--spring can be used to configure and assemble simple components into complex applications. In spring, application objects are combined declaratively, typically in an XML file. Spring also provides a number of basic functions (transaction management, persistence framework integration, etc.), leaving the development of application logic to you. All of these features of spring enable you to write cleaner, more manageable, and easier-to-test code. They also provide basic support for the various modules in spring.     SSH framework hibernate       Hibernate is an open-source object-relational mapping framework that provides JDBC with a very lightweight object encapsulation that allows Java programmers to manipulate databases with arbitrary object programming thinking. Hibernate can be applied to any JDBC application, both in Java client applications and in servlet/jsp Web applications, and most revolutionary of all, hibernate can replace CMP in the EE architecture of the EJB application. The task of achieving data persistence. There are 5 core interfaces for       hibernate: Session, Sessionfactory, Transaction, query, and configuration. These 5 core interfaces will be used in any development. Through these interfaces, you can not only access persistent objects, but also enable transaction control. These five core interfaces are described separately below. Session Interface: The session interface is responsible for performing crud operations on persisted objects (the task of crud is to complete the communication with the database, including many common SQL statements. )。 However, it is important to note that the session object is non-thread-safe. At the same time, Hibernate's session differs from the HttpSession in JSP applications. When the term "session" is used, it is actually referring to the session in Hibernate, and later the HttpSession object is called the user session.      Sessionfactory interface: The Sessionfactory interface is responsible for initializing hibernate. It acts as a proxy for the data storage source and is responsible for creating session objects. The factory model is used here. It is important to note that Sessionfactory is not lightweight, because in general, a project typically requires only one sessionfactory, and when you need to manipulate multiple databases, you can specify a sessionfactory for each database.     Config interface: The configuration interface is responsible for configuring and starting Hibernate, creating Sessionfactory objects. During Hibernate startup, the instance of the configuration class first locates the map document location, reads the configuration, and then creates the Sessionfactory object.      Transaction interface: The transaction interface is responsible for transaction-related operations. It is optional, and developers can also design their own underlying transaction-processing code.     Query and Criteria interface: query and Criteria interfaces are responsible for executing various database queries. It can be expressed in either the HQL language or the SQL statement.

Hibernate (Object Relational Mapping framework for open source)

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.