Bom dia amigos, estou encontrando dificuldades em integrar o Spring com o JPA, está gerando o clássico erro de NullPointException, e não consigo identificar, uma vez que revisei toda a configuração do applicationContext.xml.
Segue abaixo o conteúdo do arquivo e em seguida a classe:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory">
</property>
<property name="dataSource" ref="dataSource">
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://192.168.0.98:3306/noticia">
</property>
<property name="username" value="root">
</property>
<property name="password" value="root">
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="noticia">
</property>
<property name="dataSource" ref="dataSource">
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
</bean>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL">
</property>
<property name="showSql" value="true">
</property>
<property name="generateDdl" value="true">
</property>
</bean>
</property>
</bean>
<bean id="loginDao" class="br.persistence.LoginDAO">
<constructor-arg ref="entityManagerFactory">
</constructor-arg>
</bean>
Classe LoginDAO:
package br.persistence;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import br.entity.Usuario;
public class LoginDAO {
private EntityManager entityManager;
private EntityManagerFactory entityManagerFactory;
public LoginDAO() {
}
public LoginDAO(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
this.entityManager = this.entityManagerFactory.createEntityManager();
}
public Boolean validaLogin(String login, String senha) {
Query query = this.entityManager.createQuery("SELECT u FROM Usuario u WHERE u.login = :login AND u.senha = :senha");
query.setParameter("login", login);
query.setParameter("senha", senha);
Usuario user = null;
try {
user = (Usuario) query.getSingleResult();
} catch (NoResultException e) {
e.printStackTrace();
}
return (user != null);
}
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
}
O erro acontece na chamada do método validaLogin.
Agradeço a ajuda.