Bom dia pessoal,
eu tenho os seguintes códigos:
um método para verificação.
public boolean verificarExistenciaConta(String agencia, String conta){
boolean existeConta = false;
ContaBancaria contaBancaria = new ContaBancaria();
Query query = getEntityManager().createNamedQuery("procurarConta");
query.setParameter(1, agencia);
query.setParameter(2, conta);
contaBancaria = (ContaBancaria) query.getSingleResult();
if (contaBancaria!=null)
existeConta=true;
return existeConta;
}
e o meu dao genérico é o seguinte:
package br.com.fic.bancoFic.dao;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Transactional(readOnly=true,propagation=Propagation.REQUIRED)
public class DAOGenericoImpl<T,ID extends Serializable> implements DAOGenerico<T,ID> {
private EntityManager entityManager;
private final Class<T> oClass;
@SuppressWarnings("unchecked")
public DAOGenericoImpl() {
this.oClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public Class<T> getObjectClass(){
return this.oClass;
}
@Override
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public T salvar(T object) {
getEntityManager().clear();
getEntityManager().persist(object);
return object;
}
@Override
public T pesquisarPorId(Serializable id) {
return (T)getEntityManager().find(oClass, id);
}
@Override
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public T atualizar(T object) {
getEntityManager().merge(object);
return object;
}
@Override
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public void excluir(T object) {
object =getEntityManager().merge(object);
getEntityManager().remove(object);
}
@Override
public List<T> todos() {
return null;
}
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.entityManager = em;
}
public EntityManager getEntityManager() {
if(entityManager == null)
throw new IllegalStateException("Erro");
return entityManager;
}
}
Aqui está meu applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="conexao"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven/>
<context:component-scan base-package="br.com.fic.bancoFic"/>
<context:annotation-config/>
<bean id="contaDao"
class="br.com.fic.bancoFic.dao.ContaDAOImp" />
</beans>
e está dando esse erro aqui:
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalStateException: Erro
at br.com.fic.bancoFic.dao.DAOGenericoImpl.getEntityManager(DAOGenericoImpl.java:77)
Quando ele tenta dar um getEntityManager, ele vem NULL.
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.entityManager = em;
}
public EntityManager getEntityManager() {
if(entityManager == null)
throw new IllegalStateException("Erro");
return entityManager;
}
Alguém saberia me ajudar ?