Spring + hibernate versus Spring Data JPA: Are they different?

前端 未结 4 920
既然无缘
既然无缘 2021-01-31 02:10

Although not novice, I am trying to learn spring framework (again!) in order to be sure that I really understand this. I have got fair idea on core Spring (DI). Now, I am focusi

4条回答
  •  孤独总比滥情好
    2021-01-31 02:51

    when you talk about spring + hibernate

    1. If you read hibernate alone, you would understand it uses mapping(which basically configuration for mapping of pojo with database relations) and configuration(configuration specific to database like driver class, url, username, password, dialect etc.).
    2. So now if you want to use read, write, update etc. you have to get hibernatesessionfactory, open transaction and commit. Lot of pre and post work for each operation.
    3. When you integrate hibernate with spring, spring uses this configuration and keep it in application context, and provide wrapper which is hibernatetemplate which internally using hibernatesessionfactory. So you dont need to care much about pre and post code while doing these operations.
    4. It also provided caching(first and second level cache) to improve performance.
    5. And also give you to work with HQL which is independent of database.It uses database dialect to generate database specific sql.

    Now lets talk about spring + data jpa

    1. it comes with base repository interfaces(from spring data common, spring jpa)
    2. So suppose you are interested in doing crud operation, just extend crud repository, and spring will inject its implementation at run time.
    3. Lets say you want to define common method for your application, you can do that by creating new repository interface which extends Repository interface. And can use this across the application.
    4. It also provide query methods which allow you to use native sql or jpql.

提交回复
热议问题