SpringData vs EntityManager injection [NullPointerException]
I´m using spring data, witch is very good and all. But it fails me because I cant create a dinamic query
(for filtering, in an datatable, por e.g). Soo, i need to get a reference of EntityManager to build my query.
But i get nullPointerException!! Check out my config;
Code:
<!-- Autoscan for annotations -->
<context:component-scan base-package="br.com.noronha" />
<jpa:repositories base-package="br.com.noronha" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/myfinancesweb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="br.com.noronha.model.entity" />
<property name="persistenceProvider">
<bean class="org.hibernate.ejb.HibernatePersistence" />
</property>
<property name="jpaProperties">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<entry key="hibernate.hbm2ddl.auto" value="update" />
</map>
</property>
</bean>
Now here's how i´m tryin to inject it:
Code:
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this. entityManager = entityManager;
}
@Autowired
public GrupoController(GrupoService grupoService, MessageUtil messageUtil) throws Exception {
super(grupoService, Grupo.class,messageUtil);
lazyModel = new BaseLazyModelJPA<Grupo, Integer>(grupoService,entityManager, Grupo.class, messageUtil);
}
and when I try to use it, in my BaseLazyModelJPA class:
Code:
private EntityManager entityManager;
//ommited
public BaseLazyModelJPA(BaseService<T, PK> baseService, EntityManager entityManager, Class<T> entityClass, MessageUtil messageUtil) {
this.entityManager = entityManager;
//ommited
}
//this method is called by primefaces, in p:datatable.... but when it reaches the last line of the code block,
// null pointer ;(
@Override
public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
List<T> data = new ArrayList<T>();
String noneSelected = messageUtil.getMessageBundle("global.selecione");
// Criteria
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
Can anybody help?