slnowak
slnowak

Reputation: 1919

Spring-Data-JPA in CDI environment?

Does anyone tried integrating spring-data-jpa with java-ee application? I'm using glassfish3 as an application container.

I followed an official spring-data-jpa tutorial and created a class:

public class EntityManagerFactoryProducer {

    @Produces
    @ApplicationScoped
    public EntityManagerFactory createEntityManagerFactory() {
        return Persistence.createEntityManagerFactory("myPU");
    }

    public void close(@Disposes EntityManagerFactory entityManagerFactory) {
        entityManagerFactory.close();
    }

    @Produces
    @RequestScoped
    public EntityManager createEntityManager(EntityManagerFactory entityManagerFactory) {
        return entityManagerFactory.createEntityManager();
    }

    public void close(@Disposes EntityManager entityManager) {
        entityManager.close();
    }
}

But when I try to deploy my application, I'm getting an exception:

Error occurred during deployment: Exception while preparing the app : Could not resolve a persistence unit corresponding to the persistence-context-ref-name [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean/entityManager] in the scope of the module called [App]. Please verify your application.. Please see server.log for more details.
Command deploy failed.

What am I missing? Should I also have another configuration file or maybe some xml file?

Upvotes: 2

Views: 3149

Answers (2)

JR Utily
JR Utily

Reputation: 1842

It's a bit more tricky that what is told in official documentation. To handle properly a Spring Repository in a CDI env, you need to declare:

  • a dependent entity manager producer

    @Produces @Dependent @PersistenceContext 
    EntityManager entityManager;
    
  • a eager repository

    @Eager public interface TestRepository extends CrudRepository<TestEntity, Long>
    

Then you'll be able to @Inject the repository in a CDI managed Bean. If you don't use the @Dependent and the @Eager annotation, Spring will cause exceptions at the initialization of the repositories, leading to uncatch expcetions on the first request made against it.

References:

Upvotes: 1

Gandalf
Gandalf

Reputation: 2348

Since you are in a Java EE Application Container you do not want to create your own Persistence instance. The example from the Spring Data documentation you used is for CDI environmets that do not have built in JPA support. Glasfish creates EntityManagerFactory and EntityManager for you. You only need to republish it as CDI bean. So in your case it is important to use the second example shown in the documentation:

public class EntityManagerProducer {

    @Produces
    @RequestScoped
    @PersistenceContext
    private EntityManager entityManager;
}

Upvotes: 3

Related Questions