Hibernate configuration Files go to

Source: Internet
Author: User

Now we know a concept Hibernate session, only the POJO under the session management has the ability to persist. Hibernate transforms this object-oriented operation into a persistent operation when the application performs operations on an POJO instance under Session Management.

Hibernate's brief architecture is as follows:

By being able to discover hibernate requires a hibernate.properties file that is used to configure hibernate and database connection information. An XML file is also required that determines the relationship between persisted classes and data tables and data columns.

In addition to using the Hibernate.properties file, you can also use a different form of configuration file: *.cfg.xml file. In practical applications, the use of XML configuration file is more extensive, the nature of the two configuration files is the same.

Hibernate's persistence solution frees users from bare JDBC access, without the need to focus on the underlying JDBC operations, but rather the persistence layer in an object-oriented manner. The access to the underlying data connections, the implementation of data access, and transaction control all require no user concern. This is a "comprehensive solution" architecture scheme that abstracts the application layer from the underlying JDBC/JTA API. Configure the configuration file to manage the underlying JDBC connection, allowing Hibernate to resolve persistent access implementations. An architectural diagram of this "comprehensive solution" scenario:

For the above hibernate comprehensive solution architecture diagram:

(1)sessionfactory: This is the key object of Hibernate , which is a compiled memory image of a single database mapping relationship, and it is thread-safe . It is the factory that generates the session itself to be applied to ConnectionProvider, which provides an optional level two cache of data that can be reused between those transactions at the level of the process and the cluster.

(2)Session: It is a single-threaded object that interacts between the application and the persistent storage layer. It is also a key object of Hibernate persistence, and all persistent objects must be managed by the session to be persisted. This object has a short lifetime, which hides the JDBC connection and is also a transaction factory. The session object has a first-level cache, and the data for all persisted operations is in the cache at the session object before the actual flush is performed.

(3) Persistent object : Once the system-created Pojo instance is associated with a specific session and corresponds to a specified record of the data table, the object is persisted, and this series of objects is called a persisted object. Modifications to persisted objects in the program are automatically converted to persistent layer modifications. Persistent objects can be just plain Java Beans/pojo, the only peculiarity being that they are associated with the session.

(4) transient and off-tube objects : The Java instance created by the system for the New keyword is not associated with a session and is in a transient state at this time. A transient instance may be an object that has not been persisted after being instantiated by the application. If an instance has been persisted today, it is converted to a de-tube state because of the closing of the session.

(5) transaction (Transaction): Represents an atomic operation, which has the concept of a database transaction. But it uses abstraction to separate applications from the underlying specific JDBC, JTA, and CORBA transactions. In some cases, a session may contain multiple transaction objects. Although transactional operations are optional, all persisted operations should be performed under transaction management, even if they are read-only.

(6) connection provider (ConnectionProvider): It is the factory that generates the JDBC connection and the connection pool. He isolates the underlying datasource and DriverManager through abstraction. This object does not have to be accessed directly by the application and is only used when the application needs to be expanded.

(7) Transaction Factory (Transactionfactory): He is the factory that generates instances of transaction objects. The object also does not require direct access to the application.

Hibernate persistence is inseparable from the Sessionfactory object, which is a compiled memory image of the entire database mapping relationship, and the object's Opensession () method opens the Session object. Sessionfactory is generated by the configuration object.

Each hibernate profile corresponds to a configuration object. In extreme cases, you can create a configuration object without using any profile.

I. Creating a Configuration Object

The org.hibernate.cfg.Configuration instance represents an application-to-SQL database mapping configuration, which provides a buildsessionfactory () method, This method can produce an immutable Sessionfactory object.

You can instantiate the Configuration directly to get an instance and specify a hibernate mapping file for it, and if the mapping file is in the class load path, you can use AddResource () method to add a mapping definition file. So the question now is how to create a Configuration object?

As with the configuration files that Hibernate uses, you can create configuration objects in different ways. There are usually several ways to configure hibernate:

The first is to use the Hibernate.properties file as the configuration file.

The second is to use the Hibernate.cfg.xml file as the configuration file.

The third is to create a configuration object in a coded manner without using any of the config files.

Note:the only purpose of the configuration object is to create an sessionfactory instance, so it is designed to be the startup object, and once the Sessionfactory object is created, it is discarded.

1. Using hibernateproperties as the configuration file

For hibernate.properties as a configuration file, the comparison is suitable for beginners. Because it is often difficult for beginners to remember the format of the configuration file, and which properties need to be configured. Under the ETC path of the Hibernate release package, a hibernate.properties file is provided that lists all the properties of hibernate. Each configuration segment gives a rough comment, and the user can quickly configure hibernate and database links by simply canceling the comments on the desired configuration segment here's how to create a configuration object using the Hibernate.properties file.

Instantiate the Configuration object configuration cfg = new configuration ()//Call the AddResource () method multiple times to add a mapping file. AddResource ("Item.hbm.xml" ). AddResource ("Bid.hbm.xml");

View Hibernate.properties file discovery, the file does not provide a way for hibernate mapping files. Therefore, when using the Hibernate.properties file as the configuration file, you must use the CONFIG. AddResource () method to add the mapping file.

Note: as shown in the previous code, it is easy to configure hibernate properties with the Hibernate.properties file, but because you want to add the mapping file manually, this is a very tear-off when the mapping file is extremely long. This is the reason why the hibernate.properties file is not often used as a configuration file in real-world development.

There is, of course, another policy to add a configuration file, because the mapping file and the persistence class are one by one, and you can add persistent classes through the configuration object, allowing hibernate to search for the mapping file itself.

Instantiate the Configuration object configuration cfg = new configuration)//Call the AddClass () method multiple times and add the persisted class directly. addclass (PPP. Item.class). addclass (PPP. Bid.class);

2. Using Hibernate.cfg.xml as the configuration file

The situation where hibernate.properties is used as a configuration file is already seen earlier. Because the hibernate mapping file has been added to Hibernate.cfg.xml, creating a Configuration object instance with this profile is implemented by the following code:

Instantiating a configuration object the configuration cfg = new configuration ()//configure () method will take care of loading the Hibernate.cfg.xml file. Configure ()

It is important to note that after you create a configuration object through the New keyword, don't forget to call the Configure () method.

Two. Hibernate.properties and Hiberntae.cfg.xml files

If you use the Hibernate.properties file under the ETC path as a template for the profile, modifying this template file as a hibernate profile is indeed a quick way to get to hibernate development. For actual development, however, the Hibernate.cfg.xml file is typically used as a configuration file.

After deep comparison of hibernate.properties and hibernate.cfg.xml files, look at one of the following hibernate.properties configuration properties:

Specifies the dialect of the database Hibernate.dialect Org.hibernate.dialect.MySQLDialect

The above line of code is typical of the Properties file's format, the preceding key is Hibernate.dialect, and the following value is for Org.hibernate.dialect.MySQLDialect.

Next we'll look at the corresponding configuration code in the hibernate.cfg.xml file:

<property name = "dialect" >org.hibernate.dialect.MySQLDialect</property>

The dialect property of hibernate is also specified as Org.hibernate.dialect.MySQLDialect. Comparing files in two formats, it can be found that the format is different but the substance is exactly the same.

Hibernate configuration Files go to

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.