Pessoal, estou com um problema que tá me deixando louco.
é o seguinte:
tenho uma aplicação que está usando dataSource JNDI, quero que apenas o persistence.xml spring security acesso o banco. Porém quando faço o teste me retorna a seguinte exception:
null
java.lang.NullPointerException
at br.com.crweb.model.persistence.GenericDAO.save(GenericDAO.java:29)
at br.com.crweb.teste.TesteHibernateConnection.main(TesteHibernateConnection.java:21)
meu aquivo persistence.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="pu_requisicao" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:/comp/env/jdbc/requisicaoDataSource</non-jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
genericDAO:
package br.com.crweb.model.persistence;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import br.com.crweb.model.util.JPAUtil;
public class GenericDAO<T> implements IDAO<T>, Serializable{
private EntityManager em;
private T obj;
public GenericDAO(T obj) {
this.setObj(obj);
}
public GenericDAO() {
em = JPAUtil.abreConexao();
}
public void save(T obj) throws Exception {
em.getTransaction().begin();
em.persist(obj);
em.getTransaction().commit();
em.close();
}
public void update(T obj) throws Exception {
em.getTransaction().begin();
em.merge(obj);
em.getTransaction().commit();
em.close();
}
public void delete(T obj) throws Exception {
em.getTransaction().begin();
em.remove(obj);
em.getTransaction().commit();
em.close();
}
@SuppressWarnings("unchecked")
public T findById(Integer id) throws Exception {
return (T)em.find(this.getClass(), id);
}
@SuppressWarnings("unchecked")
public List<T> findAll() throws Exception {
Query query = em.createQuery("from " + getClass().getName() + "t");
//Query query2 = em.createQuery("select u from Usuario u");
return query.getResultList();
}
public T getObj() {
return obj;
}
public void setObj(T obj) {
this.obj = obj;
}
}
context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
<Resource name="jdbc/RequisicaoDataBase"
auth="Container"
type="javax.sql.DataSource"
maxAtive="100"
maxIdle="30"
maxWait="10000"
username="postgres"
password="postgres"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/requisicaoDB?autoReconnect=true"/>
</Context>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/RequisicaoDataBase" />
<property name="lookupOnStartup" value="true" />
<property name="proxyInterface" value="javax.sql.DataSource" />
</bean>
</beans>
E por último o JPAUtil:
package br.com.crweb.model.util;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAUtil {
private static EntityManagerFactory entityManagerFactory;
private static EntityManager em;
static{
entityManagerFactory = Persistence.createEntityManagerFactory("pu_requisicao");
em = entityManagerFactory.createEntityManager();
}
public static EntityManager abreConexao(){
return em;
}
}
Por favor me ajudem, obrigado.