Home > Net >  Hibernate session factory and repository confusion
Hibernate session factory and repository confusion

Time:09-06

So there are two ways to persist an entity:

  1. Using Hibernate's session factory where we get the current session and call save(), get(), update() methods.

  2. Extending JPA's repository interfaces

I have the following questions:

  1. How are these two methods different in the context of using Hibernate. As far as I understand, Hibernate is an implementation of JPA API. So when I say I want to use Hibernate, does it mean that I can use both of the above methods?
  2. What is the preferred way of the two based on convenience, flexibility and optimisation?

CodePudding user response:

JPA repository behind the scenes uses Hibernate or JPA APIs to implement its functionality and tries to "abstract" it or provide convenience methods on top of it. You don't have to use JPA repositories though, and can always switch to the Hibernate or JPA APIs when needed. Think though, if you really gain any benefit by using the Spring Data JPA repository concept.

  • Related