The relationship between SpringData Jpa, Hibernate, Jpa

 

refer to:

http://blog.csdn.net/u014421556/article/details/52635000

http://blog.csdn.net/anxpp/article/details/51415698

 

 

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 only formulates some specifications and provides some programming API interfaces, but the specific implementation is provided by the service provider. The bottom layer of the JBoss application server uses Hibernate as the implementation of JPA.

 

Since JPA is a specification - that is to say, only some interfaces are provided in the JPA specification, it is obvious that the interfaces cannot be used directly. Although the application program can be oriented towards the interface, the bottom layer of JPA must need some kind of JPA implementation, otherwise JPA still cannot be used.

 

From the author's point of view, the reason why Sun proposed the JPA specification is to unify the specifications of various ORM frameworks in an official capacity, including the famous Hibernate, TopLink and so on. However, the JPA specification brings good news to developers: developers face the interface of the JPA specification, but the underlying JPA implementation can be switched at will : if you think Hibernate is good, you can choose Hibernate JPA implementation; if you think TopLink is good, you can choose TopLink JPA implementation... …so that developers can avoid learning an ORM framework for using Hibernate and another ORM framework for using TopLink.

 

The following figure shows the relationship between JPA and ORM frameworks such as Hibernate and TopLink:



 

 The relationship between JPA specification and ORM framework

 

The relationship between JPA and Hibernate is like the relationship between JDBC and JDBC drivers . JPA is a specification. In addition to being an ORM framework, Hibernate is also a JPA implementation. How does JPA replace Hibernate? Can JDBC drive JDBC drivers?

 

 

 

So how does Spring Data JPA relate to the JPA specification?

 

Implementing an application's data access layer has been cumbersome for quite some time. Too much boilerplate code has to be written. Domain classes, are not designed to be oriented in a true object or domain-driven manner.

Using spring data jpa can make persistent development of rich Domain classes a lot easier, even though the amount of boilerplate code to implement the repository is quite high. So the goal of Spring data jpa is to simplify efforts regarding the various persistent storage data access layers.

 

Note: Domain classes refers to POJO classes . For example, there is a table in the database: Student, then we will define the corresponding Student.java in the program, and this Student.java belongs to Domain classes.

 

Long story short, Spring Data JPA provides the implementation of the Repository layer based on the JPA specification , but you need to decide which ORM to use.

My understanding is that although all ORM frameworks implement the JPA specification, there are some differences in the code that needs to be written to switch between different ORM frameworks, and by using Spring Data Jpa, it is convenient for everyone to switch between different ORM frameworks instead of Change the code. And Spring Data Jpa encapsulates the Repository layer very well, which can save a lot of trouble.



 

 

one example

 If a table user has three fields, id, name and age, to find users whose surname is above a certain age, in traditional Spring+Hibernate, we write the dao layer like this:

 

UserDao:

    public interface UserDao{
List<User> findByNameLikeAndAgeGreaterThan(String firstName,Integer age);
}

 

UserDaoImpl:

public class UserDaoImpl implements UserDao{
@Override
public List<User> findByFirstNameAndAge(String firstName, Integer age) {
//具体hql查找:"from User where name like '%'"+firstName + "and age > " + age;
return hibernateTemplateMysql.execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException {
String hql = "from User where name like '?' and age > ?";
Query query = session.createQuery(hql);
query.setParameter(0, firstName+"");
query.setParameter(1, age);
return query.uniqueResult();
}
});
}
}

 

 However, what if we use Spring Data JPA:

 

UserDao:

public interface UserDao extends JpaRepository<User, Serializable>{
List<User> findByNameLikeAndAgeGreaterThan(String firstName,Integer age);
}

 

 Yes, that's it, there is no more, you do n't even need to write the implementation ! The service directly calls userDao.findByNameLikeAndAgeGreaterThan(firstName+"%",age).

That's it.

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326022723&siteId=291194637