Search code examples
springjpajava-ee-6data-access-layerspring-data

spring data like functionality for JEE6


I have experience building applications on the Spring Framework primarily. I was wondering if there was anything similar to the Spring Data API (to support a Data Access Layer) in the JEE6 space?

I know I can wire in an entity manager like:

@PersistenceContext 
EntityManager em;

Ideally I would like to avoid writing reams of boiler plate JPA code on Data Access beans, is an API similar to SpringJPA which can help cut down on the amount of boilerplate code such as findAll(), findByX() etc. For example, with SpringJPA I can define a bean as:

@Repository
public interface FooRepository 
    extends JpaRepository<Foo, String>
{

}

Whereas in vanilla JEE6 I would need a

  • a FooRepository interface with methods Foo findOne(Long), List<Foo> findAll()
  • a FooRepositoryImpl which implements the interface and interacts with the EntityManager

Solution

  • Spring Data JPA ships with a CDI extension to simply @Inject a repository into your CDI managed bean. See the reference documentation for details. The approach still requires Spring JARs on the classpath but no container being bootstrapped. This functionality is also available for MongoDB repositories.