Home > Enterprise >  Spring Data JPA with hibernate annotations
Spring Data JPA with hibernate annotations

Time:05-02

We have a large code base using Hibernate and JPA with hand-made DAO Layer. Entites are mapped with JPA annotations (javax.persistence) and if needed, hibernate-specific annotations (org.hibernate.annotations). We are at least using the following hibernate-specific annotations:

import org.hibernate.annotations.Immutable;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;

We want to move to Spring Data JPA for the DAO Layer. Can we use Spring Data JPA for entites annotated with JPA and Hibernate annotations?

Spring Data JPA comes with its own annotations, and it's unclear to me what we can mix or not.

CodePudding user response:

Spring Data JPA is built on top of the JPA API which mainly allows you to abstract away from using JPA 's EntityManager directly and provide a declarative way to define the JPQL/native SQL to query the entities. In the end , it will use EntityManager to manage the entities under the cover.

So your question is just equivalent to asking can we use JPA 's EntityManager to manage the entity that are annotated with the hibernate specific annotations . The answer is yes and it is very normal to do so if you want to use some hibernate specific feature that is not defined in the JPA specification.

So I don't see any problems that you cannot do it.

This tutorial is one of the example. It shows that we can use the EntityManager API to work with an entity that is annotated with @LazyCollection.

CodePudding user response:

Spring also uses the annotations from JPA to process into Hibernate, no change would be necessary

  • Related