A summary of Hibernate

Source: Internet
Author: User

Previously done. NET, the recent Java project, responsible for the development of the server, direct use of JDBC, thread safety issues, cache synchronization problems and connection pooling, are manual writing, not only cumbersome and error prone. At the end of the project, take the time to learn the next hibernate, write a few demos every night, a week down, finally to hibernate has a whole understanding.

Hibernate as an ORM framework, referring to my recent projects (loaded cache more, which is often used without the need to modify the data), Hibernate provides a level two caching mechanism let me see the Light ' (*∩_∩*), from the level two cache say I understand hibernate.

Hibernate level two cache, in short, is a cache of sessionfactory levels. What do you mean? Understand the sessionfactory is something, basically understand the level two cache is a what ghost. Sessionfactory, create a session of the factory, we all know what the session is, then if we come to design an ORM, then how to design sessionfactory? Of course, sessionfactory to load the database connection configuration, data object metadata, these two are necessary, if sessionfactoy do not know the database connection string, but also talk about what Orm, so say, Sessionfactory is the starting point of hibernate, it loads all the initialization configuration, and once the load is not allowed to modify, from here can understand that sessionfactory is thread-safe, and then more threads share their data will not have problems, can not be modified to determine this. Typically an application will create a singleton sessionfactory when it is launched, as to why, obviously. Although the second-level cache is sessionfactory level, note that it belongs to its level, not sessionfactoy, because Hibernate does not implement it, but instead uses other cache products, such as Ecache. What data fits into a level two cache? This will be our own grasp, the basic is some commonly used and long time not modified data, once loaded on OK, to avoid frequent access to the database. Because the level is high, and does not modify (generally do not modify, set to Read-only mode, read-write test use on it), so can be a lot of sessions to share.

Say the level two cache, now say the first level cache, what is a level of cache? In Hibernate, the primary cache refers to the session cache, how does the session cache work? Starting with three concepts, hibernate objects can be divided into three states: transient (transient, also called temporary), persistent (persistent , also called persistent object), Free State (detached). A temporary object is generally new, persistent objects are generally from the database get out, the free object is generally discarded by the session (evict). The session cache is the one in which the data from the database is kept and synchronized at any time, guaranteeing the same data as the database. This requires an identity that uniquely identifies who this object is, and this identity is Object-identifier (OID). A temporary object through save, Persit, Saveorupdate will become persisted object, a persistent object through evict, close, clear and so on will become Free State, a free object through update, Saveorupdate can also be changed back to persisted objects. Of course, various methods have their own specific conditions and restrictions, some places need to be aware that there is no more to say here. In short, the session cache, hibernate in a session with the implementation of the database interoperability with the policy mechanism, whether it is deferred loading, transaction submission flush and so on is to better maintain the caching mechanism, it can be said that this session mechanism is the soul of hibernate.

Hibernate's relational mappings include one-to-many, one-to-one, many-to-many, and inheritance mappings, which can of course be bidirectional. Similarly, each of the three maps has its own characteristics, if to remember its mapping method, such as Many-to-one, one-to-one and other configurations, inevitably fall into the turned out. Can stand in the designer's point of view, if we design a pair of multi-mapping, how to play it? First, the "1" side, if necessary, to maintain a set of it; "n" must maintain a "1" side of the object bar, because the foreign key must be located in the N-side. The mapping file is out, 1 parties need a set, and to tell him which table, the foreign key (key) is who, the corresponding object (One-to-many) is who, and then set the Inverse=true control to the parties, to avoid repeated updates. More than one side, how to find the 1-side of that association object, first of all to know the class name of the associated object (class), and then to know which columns (column). Of course, there are some cascade, caches and other details configuration and so on. The above is simply summed up under the Hiberate map, know that it can be OK, the specific use of the time must be reference to the document.

Hibernate provides three ways to query: HQL, QBC, and local SQL. HQL is a semi-object-oriented query language for hibernate-like SQL queries only, so he is semi-object oriented because it references the way SQL queries the language, and HQL syntax is Object-like "from Employee where Ename=: Ename ", where the employee is not a table name, is a class name, and Ename is not a field name in the database but an object property name. QBC query is completely object-oriented, he through some static classes to implement qualified queries, statistical queries, paging sorting and so on. Local SQL query, it is directly write the standard SQL statement to query.

Finally, there are Sesison management, hibernate basically provides three kinds of ways to manage the session: Thread binding, JTA transaction Management, hibernate delegation management, only one thread binding management is learned. Thread binding session, the meaning of the binding is a one-to-one, a thread corresponding to a session, so do not consider the problem of data sharing, thread safety, in addition to the same thread transaction in the DAO layer of multiple methods to manage, convenient!

Learning is about summing up thinking, over.

  

A summary of 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.