Boa noite galera.
Estou tentando fazer um simples insert em uma tabela pessoa com os seguinte campos ( nome , idade ).
Estou tendo muita dificuldade mas já fiz uma boa parte.
Esta dando o seguinte erro:
01:01:54,135 INFO [stdout] (http-localhost/127.0.0.1:8080-2) Erro ao tentar inserir pessoa null
01:01:54,136 INFO [stdout] (http-localhost/127.0.0.1:8080-2) java.lang.NullPointerException
Classe Abstrata:
package com.rafael.dao;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public abstract class AbstractDao extends HibernateDaoSupport {
public AbstractDao() {
super();
}
public AbstractDao(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
public void savePessoa(Object entity) throws Exception {
getHibernateTemplate().save(entity);
}
}
PessoaModel:
[code]package com.rafael.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name=“pessoa”)
public class PessoaModel {
@Column(name="id")
@Id
private int id;
@Column(name="nome")
private String nome;
@Column(name="idade")
private int idade;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public int getIdade() {
return idade;
}
public void setIdade(int idade) {
this.idade = idade;
}
}
[/code]
PessoaDao:
[code]package com.rafael.dao.impl;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.rafael.dao.AbstractDao;
import com.rafael.dao.PessoaDao;
import com.rafael.model.PessoaModel;
@Repository
public class PessoaDaoImpl extends AbstractDao implements PessoaDao {
public PessoaDaoImpl(){
super();
}
public PessoaDaoImpl(SessionFactory sessionFactory){
super(sessionFactory);
}
@Override
public void savePessoa(PessoaModel pessoaModel) {
try {
// entityManager.persist(receivedBatch);
savePessoa(pessoaModel);
System.out.println(“Pessoa Inserida com sucesso!”);
} catch (Exception e) {
System.out.println("Erro na classe: ReceivedBatchDaoImpl "+ e.getMessage());
}
}
}
[/code]
Service:
package com.rafael.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rafael.dao.PessoaDao;
import com.rafael.model.PessoaModel;
import com.rafael.service.PessoaService;
@Service("pessoaServiceImpl")
public class pessoaServiceImpl implements PessoaService{
@Autowired
private PessoaDao pessoaDao;
@Override
public void savePessoa(PessoaModel pessoaModel) {
try {
pessoaDao.savePessoa(pessoaModel);
System.out.println("Pessoa Cadastrada com sucesso!");
} catch (Exception e) {
System.out.println("Erro ao tentar cadastrar Pessoa!");
}
}
public PessoaDao getPessoaDao() {
return pessoaDao;
}
public void setPessoaDao(PessoaDao pessoaDao) {
this.pessoaDao = pessoaDao;
}
}
Arquivo de propriedade:
username=root
password=root
url=jdbc:mysql://localhost/pessoas
driver=com.mysql.jdbc.Driver
springFile=spring/SpringBeans.xml
Pegando dados do arquivo de propriedades:
package com.rafael.util;
public class Constants {
//TesteSpring configuration file
public static final String TESTESPRING_FILE = "testeSpring.properties";
//Spring configuration file
public static final String SPRING_FILE = "springFile";
//Spring beans
public static final String USER_DAO = "pessoaDao";
public static final String USER_SERVICE = "pessoaService";
}
PropertiesUtil
package com.rafael.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtils {
private static PropertiesUtils instance;
private static Properties properties;
private PropertiesUtils() {
loadProperties();
}
public static PropertiesUtils getInstance() {
if (instance == null) {
instance = new PropertiesUtils();
}
return instance;
}
private void loadProperties() {
properties = new Properties();
try {
InputStream inputStream = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("./" + Constants.TESTESPRING_FILE);
properties.load(inputStream);
inputStream.close();
} catch (IOException e) {
System.out.println(e);
}
}
public String getValue(String key) {
return properties.getProperty(key);
}
public String getSpringFile() {
return getValue(Constants.SPRING_FILE);
}
}
SpringUtil:
package com.rafael.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringUtils {
private static SpringUtils instance;
private ApplicationContext context;
private SpringUtils() {
String path = PropertiesUtils.getInstance().getSpringFile();
this.context = new ClassPathXmlApplicationContext(path);
}
public static SpringUtils getInstance() {
if (instance == null) {
instance = new SpringUtils();
}
return instance;
}
public Object getBean(String beanName) {
return this.context.getBean(beanName);
}
}
DataSuorce:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>testeSpring.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
</beans>
SpringBeans:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >
<import resource="DataSource.xml" />
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.rafael.model.PessoaModel</value>
</list>
</property>
</bean>
<!-- Service -->
<bean id="pessoaService" class="com.rafael.service.impl.PessoaDaoImpl">
<property name="pessoaDao" ref="userDao" />
</bean>
<!-- Daos -->
<bean id="pessoaDao" class="com.rafael.dao.impl.PessoaDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
PessoaView:
package com.rafael.view;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import com.rafael.model.PessoaModel;
import com.rafael.service.PessoaService;
@ManagedBean(name = "pessoa")
@Scope("request")
public class PessoaView {
@Autowired
private PessoaService pessoaService;
private PessoaModel pessoaModel;
@PostConstruct
public void init() {
pessoaModel = new PessoaModel();
}
public void savePessoa() {
try {
pessoaService.savePessoa(pessoaModel);
System.out.println("Pessoa Inserida com sucesso!");
} catch (Exception e) {
System.out.println("Erro ao tentar inserir pessoa "+ e.getMessage()+" \n"+ e);
}
}
public PessoaModel getPessoaModel() {
return pessoaModel;
}
public void setPessoaModel(PessoaModel pessoaModel) {
this.pessoaModel = pessoaModel;
}
}
XHTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Teste de Spring</title>
</h:head>
<h:body>
<h:form prependId="false">
<p:panel header="Bem-Vindo"
style="width:500px;margin-left:auto;margin-right:auto;
border: 1px solid #000000;box-shadow:10px 10px 5px black; margin-top: 250px">
<p:messages id="messages" showDetail="true" autoUpdate="true"
closable="true" />
<h:panelGrid columns="2" style="margin-left:auto;margin-right:auto;">
<h:outputLabel value="Nome:" />
<p:inputText value="#{pessoa.pessoaModel.nome}" />
<h:outputLabel value="Idade:" />
<p:password value="#{pessoa.pessoaModel.idade}" />
</h:panelGrid>
<h:panelGrid columns="2" style="margin-left:auto;margin-right:auto;">
<p:commandButton type="submit" id="login" value="Inserir"
action="#{pessoa.savePessoa}" ajax="false" />
<p:commandButton id="clear" value="Limpar"
ajax="false" />
</h:panelGrid>
</p:panel>
</h:form>
</h:body>
</html>
Galera sei que ta grande mas da uma força ai…
Agradeço desde já.
Abraço.