Common ORM Framework Understanding

Source: Internet
Author: User

  

Java programming is unavoidable to deal with the database, so how to operate the database efficiently and conveniently, but also a need for the problem, the native JDBC-based approach is of course very inefficient, and to write a lot of useless template code, not worth selecting. Fortunately, we do not need to reinvent the wheel, there are already a lot of good ORM framework to use, common such as Hibernate, Mybatis,toplink, JAVA EE3.0 in the specification of the JPA, is also very useful, so sometimes the interviewer will ask some and this related to the face of the question, the following to organize some personal encounters, or online often see some of the face questions.

First, what is JPA, and why should there be JPA?

The full name of JPA is the Java Persistence API, the Java Persistence API, which is an expert in EJB 3.0, and as part of the JSR-220, it can be understood as a Java standard specification in simple terms. This specification has some standard interfaces for persisting Java objects, and it can be said that

JPA is a standard ORM (Object Relational Mapping) specification.

So why is there a JPA? Nowadays there are many ORM frameworks, and different ORM frameworks are not compatible with each other. Sun also realized that the persistence of Java objects is an important content, to make this specification, on the one hand, to simplify the EJB for object persistence operation, on the other hand, but also hope that through the development of unified norms, to achieve the purpose of unified ORM standards.

Although JPA is a service for EJBS, it does not have special requirements for Java objects, it can be a normal pojo, that's all. The specification consists of three parts: ORM mapping metadata, standard interface API, and query language.

It is important to note that JPA is just an interface specification, not an implementation, and that the implementation is done by each vendor, and that the HIBERNATE,TOPLINK,OPENJPA has implemented the JPA interface well at this time.

To operate a database based on JPA, a persistence.xml configuration file is required, and the specific key classes are entitymanagerfactory, Entitymanager, through Entitymanager instances, We can operate on Java objects, such as persist, merge, remove, CreateQuery, and so on.

Second, Hibernate

Hibernate may be the most commonly used ORM framework, and overall performance is still relatively good. Had to mention his founder: Gavin King, who was said to be looking for change because he couldn't love EJB1.1 's bloated pattern code, and the first thing he did before writing hibernate was to buy a book of SQL Basics first. It also shows that to be successful, one is to think, and the other is to do it immediately.

To hibernate development, I think there are a few points to understand, one is the configuration file, the default Hibernate.cfg.xml, and the object corresponding to the configuration file, such as User.hbm.xml. In addition to JPA, Hibernate also has several important objects, such as configuration, Sessionfactory, Session, Transaction, Query, and Criteria. As long as you know how they work, it's easy to build a demo project. Here are a few common questions:

2.1 What are the two ways to create a session, and what is the difference between them?

The session object in Hibernate is mainly responsible for CRUD operations on objects, there are two ways to create a session, the first is opensession, and the other is getcurrentsession.

In the first way, a new session is always created, and when the session ends, a manual shutdown is required to free the resource, and getcurrentsession is associated with the current thread, and if the current thread does not, then a new one, if any, is taken, and the feature of this is that it does not need to be manually closed. When a transaction is committed, it is automatically closed, and then the shutdown interface is called to report an error. But this approach requires that we set the following in the configuration file

Thread

The underlying should be based on the threadlocal implementation. Normally, we can use this one.

What is the difference between 2.2 load and get?

Both load and get are records that query the specified primary key from the database. Refer to the information on the Internet, organized as follows:

The same point: the session cache will be looked up first, if there is in the cache, then return directly.

Difference: Load thinks there must be a record in the database, so if it doesn't exist, a proxy object is created, and when the object is actually used

Data, the two-level cache and the database are queried again, which is called lazy loading. If not found, return to Objectnotfoundexception. For GET, the two-level cache and database are looked up directly, and NULL is returned directly.

What are the main types of primary keys in 2.3 hibernate?

For a self-growing database, use the identity, such as MySQL, for the serialized primary key, using sequence, such as Oracle. If not clear, you can use native.

2.4 Caching mechanism in hibernate? Professional Java training is thought to be divided into two categories: the so-called first-level cache and level two cache. First-level caches are session-level and mandatory, and we don't need to be overly concerned or interfere with their use. When we use the session to the Java object CRUD operations will be used, such as adding, is to put the object into a first-level cache, this time the information is not in storage, but in the cache, when the commit is called after the storage.

The secondary cache is at the level of sessionfactory, that is, the objects in the level two cache under the same sessionfactory can be shared by all sessions, but this cache is based on external cache components and needs to be manually configured and opened. It is common to have ehcache.

Because it is shared by the session, there is a concurrency problem, so the level two cache is only suitable for storing data that is infrequently modified, non-critical, and occasionally or rarely produced concurrently.

When querying, the cache will be checked first, then the database, update the cache, and then update the database.

Hibernate also has a class of caches called query caches, which only work when queried, and this type of general purpose is not too large.

Third, Mybatis

The predecessor of MyBatis was Ibatis, an open source project from Apache. Similarly, this is an ORM framework, but this framework has the biggest features three points, one is interface-oriented programming, the other is to define the SQL statement in the configuration file, and the third is to support dynamic SQL, which is its unique side.

On the internet to find a bit, the related questions are not many, say I met the bar

3.1 MyBatis The difference between # and $?

Both of these can be used as placeholders, but the placeholders in the #{}, MyBatis, are handled, similar to those in normal SQL, so you can avoid the problem of SQL injection.

While $ is output as is, it is not possible to avoid the problem of SQL injection, usually we prefer #, but if we are going to pass in a field name, then use $, such as ORDER by \${id}

3.2 MyBatis and hibernate comparison?

The same point will not say, say the main difference:

1. MyBatis handwritten SQL, and Hibernate provides a mapping mechanism that developers do not care about

2. MyBatis control finer granularity, but portability is poor, hibernate developing DAO is very simple, portability is good

3. MyBatis easy to learn, hibernate slightly more difficult

4. Hibernate provides object-oriented SQL statement HQL and can use normal SQL, but MyBatis can only use normal SQL.

The above is a personal summary of the three of the framework of some understanding, follow-up if you encounter the relevant questions, and then add up

Common ORM Framework Understanding

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.