Daniel Acorsi
Daniel Acorsi

Reputation: 31

Cant get Spring Data JPA + CDI on Java EE 7 stack

Im trying to get Spring data JPA to work with EJB and CDI (Java EE 7). Well, I followed the documentation (http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/jpa.repositories.html#jpd.misc.cdi-integration), but still cant @inject my repository in a stateless ejb. Here are the codes:

@Configuration
@EnableJpaRepositories
public class EntityManagerFactoryProducer {

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

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

$

public interface TipoFolhaRepository extends JpaRepository<TipoFolha, Long> {

List<TipoFolha> findByNome(String nome);

TipoFolha findByTipo(String tipo);
}

$

@Stateless
public class TipoFolhaFacade extends AbstractFacade<TipoFolha> {

@Inject
TipoFolhaRepository tpRepo;

@Override
public  List<TipoFolha> findAll(){
    return tpRepo.findAll();
}
}

Follows the error. WELD-001408 Unsatisfied dependencies for type [TipoFolhaRepository] with qualifiers [@Default] at injection point [[BackedAnnotatedField] @Inject com.mycompany.ejb.TipoFolhaFacade.tpRepo]

waht i am missing ? =S

Upvotes: 3

Views: 1871

Answers (1)

Frans Oosterhof
Frans Oosterhof

Reputation: 333

You need to enable CDI with bean-discovery-mode="all" in the module where your repository classes reside. This means creating a beans.xml in the META-INF folder with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
       http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
</beans>

A short explanation of the different discovery modes can be found in this Oracle blogpost

Upvotes: 3

Related Questions