Brief introduction of JPA and Spring Data JPA and Hibernate

Source: Internet
Author: User
Tags java se

What is 1.JPA? and related overview
    • JPA is a shorthand for the Java persistence API, an ORM specification that Sun is officially proposing!
    • Sun has proposed this specification for 2 reasons:
      1. Simplify existing Java EE and Java SE application development work.
      2.Sun wants to integrate ORM technology to achieve the world.
    • The point is that JPA is a set of specifications, not a specific ORM framework.
    • Therefore, hibernate, TopLink and other ORM frameworks are implementations of JPA, where Hibernate has been certified by Sun compatibility.
    • JPA's standard customization is for hibernate authors to participate in customization, so JPA is an assembly of hibernate.
    • The advantage is:
      1. Developers are oriented to the JPA specification interface, but the underlying JPA implementation can be switched arbitrarily: It feels good to hibernate, can choose Hibernate JPA implementation, think TopLink good, you can choose TopLink JPA implementation.
      2. Developers can avoid learning a set of ORM frameworks for using Hibernate, and then learn a set of ORM frameworks for using TopLink.
    • Used in the project as: in the entity class, use @Entity , @Table , @Id and @Column other annotations.
    • The sample code is as follows:
import javax.persistence.*;import javax.persistence.Id;@Entity@Table(name = "t_user")//数据库中表名public class UserEO {    @Id    @Column(name = "USER_ID")//数据库中字段名    private String userId;    @Column(name = "USER_NAME")    private String  userName;   }
    • The jar package structure looks like this:
What is 2.Spring Data JPA? and related overview
    • First the spring framework is almost omnipotent and ubiquitous.
    • Next Spring also wants to do the persistence related work, and already has spring-data-** this series of packages (SPRING-DATA-JPA,SPRING-DATA-TEMPLATE,SPRING-DATA-MONGODB, etc.).
    • Where SPRING-DATA-JPA represents the integration of the spring framework into JPA.
    • Spring Data JPA provides the implementation of the repository layer on the basis of the JPA specification.
    • In the repository layer of the project, the specific performance is as follows:
      1. Interface to inherit JpaRepository the interface, the sample code is as follows:
@Repositorypublic interface UserRepository extends JpaRepository<UserEO,String>{//第二个参数是UserEO对应的表的主键类型    //直接定义如下方法,不需要写具体的sql语句   List<UserEO> findByUserName(String userName);    //对于自定义操作(比如更新状态字段为 ‘1’)使用 @Query注解    @Query("update UserEO u set u.status = ‘1‘ where u.userName = :userName        void updateStatusByName(String userName);    //此方法 需要拼接条件  在实现类中重写     List<UserEO> findByUserName(Condition condition);}

2. The implementation class does not need to be explicitly implemented UserRepository , as long as it is named UserRepositoryImpl .
The advantage is that there is no need to rewrite a method that does not require writing SQL or SQL statements that are not complex (using @Query annotations can be implemented).
For methods that need to be judged and spliced into query conditions, we can override them in the implementation class.
The sample code is as follows:

public  List<UserEO> findByUserName(Condition condition) {    StringBuilder jpql = new StringBuilder(" select u from userEO u where u.tatus = ‘1‘  " );    if (StringUtils.hasText(conditon.getUserName)) {            jpql.append(" and u.userName = :userName " );       }    //执行 sql .. 返回List集合...}
    • The jar package structure is as follows:

    • The use of SPRING-DATA-JPA can be consulted: https://www.cnblogs.com/dreamroute/p/5173896.html

3. Summary
    • JPA is a set of specifications, and the Hibernate framework makes a good implementation.
    • The spring framework is the repository implementation of CRUD-related operations, the spring Data JPA
    • JPA operates on an entity (Xxxeo) to associate it with a database.
    • The following 2 graphs show the JPA and ORM framework relationships, and what Spring Data JPA does

This article references: http://www.lxway.com/528201191.htm
Https://www.cnblogs.com/dreamroute/p/5173896.html
Http://www.cnblogs.com/xiaoheike/p/5150553.html
Https://baike.baidu.com/item/JPA/5660672?fr=aladdin

Brief introduction of JPA and Spring Data JPA and 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.