🗄️🌱 How to integrate JAKARTA DATA with SPRING and HIBERNATE --Inspired by Vlad Mihalcea
🗄️🌱 How to integrate JAKARTA DATA with SPRING and HIBERNATE --Inspired by Vlad Mihalcea
Jakarta Data brings a standardized repository API to the Jakarta ecosystem.
If you already use Spring Data JPA, the model will look familiar: repository interfaces, CRUD operations, derived queries and JPQL annotations.
However, with Spring, the integration is not automatic.
🔸 TLDR
Hibernate 6.6+ can implement Jakarta Data repositories, but Spring does not yet provide native transaction integration for them.
🔸 1. DEFINE THE REPOSITORY
@Repository public interface PostRepository extends BasicRepository<Post, Long> { StatelessSession getSession(); default Post persist(Post post) { getSession().insert(post); return post; } @Query(""" select p from Post p where p.title like :title """) List<Post> findByTitle( @Param("title") String title ); }
BasicRepository provides common data operations, while Hibernate uses StatelessSession behind the generated repository.
🔸 2. CONNECT IT TO SPRING
@Bean StatelessSession statelessSession( EntityManagerFactory factory) { return (StatelessSession) Proxy.newProxyInstance( StatelessSession.class.getClassLoader(), new Class[]{StatelessSession.class}, new StatelessSessionInvocationHandler(factory) ); }
Spring does not automatically bind Jakarta Data repositories to its transaction context.
A proxy is therefore required to retrieve the current EntityManager, reuse its JDBC connection and expose the matching StatelessSession.
🔸 3. DEFINE TRANSACTIONS IN THE SERVICE
@Service @Transactional(readOnly = true) public class ForumService { private final PostRepository posts; @Transactional public Post addPost(String title) { Post post = new Post(); post.setTitle(title); return posts.persist(post); } }
The service layer continues to define transaction boundaries, while the proxy connects the repository to the active Spring transaction.
🔸 TAKEAWAYS
▪️ Jakarta Data feels familiar to Spring Data JPA users
▪️ Hibernate implements repositories with StatelessSession
▪️ Spring transaction propagation requires a custom bridge
▪️ Jakarta Data is promising for Jakarta EE and Quarkus
▪️ Spring Data JPA remains the simpler choice for most Spring applications
A very interesting integration; but currently more advanced experiment than drop-in replacement.
#JakartaData #JakartaEE #SpringBoot #Spring #Hibernate #Java #JPA #SoftwareArchitecture
Go further with Java certification:
Java👇
Spring👇
SpringBook👇
JavaBook👇