SpringData-Introduction of JPA

ORM(Object-Relational Mapping)

Represents object-relational mapping.

In object-oriented software development, through ORM, objects can be mapped to relational databases.

As long as there is a set of programs that can establish the association between the object and the database, and the operating object can directly manipulate the database data, it can be said that this set of programs implements the ORM object relationship mapping.

Simply put: ORM is to establish the relationship between the entity class and the database table, so as to achieve the purpose of operating the entity class is equivalent to operating the database table.

Common orm frameworks: Mybatis (ibatis), Hibernate, Jpa

Why use ORM

When implementing an application (not using O/R Mapping), we may write very many data access layer codes, save data from the database, modify data, delete data, and these codes are all repeated.

The use of ORM will greatly reduce repetitive code. Object-relational mapping (ORM for short) mainly implements the mapping of program objects to relational database data.

hibernate overview

Hibernate is an open source object-relational mapping framework.

It has a very lightweight object encapsulation for JDBC. It establishes a mapping relationship between POJOs and database tables. It is a fully automatic orm framework. Hibernate can automatically generate SQL statements and execute them automatically, allowing Java programmers to use objects as they want. Programming thinking to manipulate the database.

JPA

The full name of JPA is Java Persistence API, that is, Java Persistence API. It is a set of ORM-based specifications launched by SUN. The internal is composed of a series of interfaces and abstract classes.

JPA describes the object-relational table mapping relationship through JDK 5.0 annotations, and persists the entity objects in the runtime to the database.

Advantages of JPA

1. Standardization

   JPA is one of the Java EE standards issued by the JCP organization. Therefore, any framework that claims to be in compliance with the JPA standard follows the same architecture and provides the same access API. This ensures that enterprise applications developed based on JPA can be modified in different ways after a small amount of modification. Run under the JPA framework;

 

2. Support for container-level features 

   The JPA framework supports container-level transactions such as large data sets, transactions, and concurrency, which makes JPA go beyond the limitations of simple persistence frameworks and play a greater role in enterprise applications;

 

3. Simple and convenient

   One of the main goals of JPA is to provide a simpler programming model: creating entities under the JPA framework is as simple as creating Java classes, without any constraints and restrictions, only the use of javax.persistence.Entity for annotations, the JPA framework and interfaces It is also very simple, there are not too many special rules and design pattern requirements, developers can easily master. JPA is designed based on non-intrusive principles, so it can be easily integrated with other frameworks or containers;

 

4. Query ability

   JPA's query language is object-oriented rather than database-oriented. It constructs query statements with object-oriented natural syntax, which can be regarded as the equivalent of Hibernate HQL. JPA defines a unique JPQL (Java Persistence Query Language). JPQL is an extension of EJB QL. It is a query language for entities. The operation object is an entity, not a relational database table, and it can support batch updates and Modification, JOIN, GROUP BY, HAVING and other advanced query features that are usually only provided by SQL, and even support sub-queries;

 

5. Advanced features

   JPA can support advanced object-oriented features, such as inheritance between classes, polymorphism, and complex relationships between classes. This support allows developers to maximize the use of object-oriented models to design enterprise applications without the need for their own Deal with the persistence of these characteristics in relational databases.

The relationship between JPA and hibernate

The JPA specification is essentially an ORM specification. Note that it is not an ORM framework—because JPA does not provide an ORM implementation, it just formulates some specifications and provides some programming API interfaces, but the specific implementation is provided by the service provider. 

JPA和Hibernate的关系就像JDBC和JDBC驱动的关系,JPA是规范,Hibernate除了作为ORM框架之外,它也是一种JPA实现。JPA怎么取代Hibernate呢?JDBC规范可以驱动底层数据库吗?答案是否定的,也就是说,如果使用JPA规范进行数据库操作,底层需要hibernate作为其实现类完成数据持久化工作。

 

 

 

Guess you like

Origin blog.csdn.net/weixin_42629433/article/details/84667198