Estou exercitando e fazendo um projeto similar em paralelo (cadastro de clientes) e quando envio o formulário preenchido, está sendo gerado outro cliente, porém nulo. Fiz alguns testes e parece que o problema é quando chamo a url /clientes , parece que ela roda duas vezes o hibernate. Praticamente é cópia do projeto da aula até o momento, porém troquei de produtos para clientes.
Obrigado
veja a saída do console abaixo quando inseri um cliente com nome Luis: //
INFORMAÇÕES: Reloading Context with name [/consultoriodentario] is completed
Luis
Wed Dec 07 14:31:52 BRST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Hibernate: insert into Cliente (celular, cpf, dataNascimento, email, nome, rg, senha, telefone) values (?, ?, ?, ?, ?, ?, ?, ?)
null
Wed Dec 07 14:31:53 BRST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Hibernate: insert into Cliente (celular, cpf, dataNascimento, email, nome, rg, senha, telefone) values (?, ?, ?, ?, ?, ?, ?, ?)
//
package br.com.consultorio.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import br.com.consultoriodentario.daos.ClienteDAO;
import br.com.consultoriodentario.models.Cliente;
@Controller
public class ClientesController {
@Autowired
private ClienteDAO clienteDao;
@RequestMapping("/clientes/clienteForm")
public String form(){
return "clientes/clienteForm";
}
@RequestMapping("/clientes")
public ModelAndView index(Cliente cliente){
System.out.println(cliente);
clienteDao.gravar(cliente);
ModelAndView modelAndView = new ModelAndView("clientes/ok");
return modelAndView;
}
}
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
public class JPAConfiguration {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factoryBean.setJpaVendorAdapter(vendorAdapter);
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUsername("root");
dataSource.setPassword("");
dataSource.setUrl("jdbc:mysql://localhost:3306/consultoriodentario");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
factoryBean.setDataSource(dataSource);
Properties props = new Properties();
props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
props.setProperty("hibernate.show_sql", "true");
props.setProperty("hibernate.hbm2ddl.auto", "update");
factoryBean.setJpaProperties(props);
factoryBean.setPackagesToScan("br.com.consultoriodentario.models");
return factoryBean;
}
@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory emf){
return new JpaTransactionManager(emf);
}
}
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import br.com.consultoriodentario.models.Cliente;
@Repository
@Transactional
public class ClienteDAO {
@PersistenceContext
private EntityManager manager;
public void gravar(Cliente cliente){
manager.persist(cliente);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="security"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tags" %>
<tags:pageTemplate titulo="Consultório ">
<!-- Bootstrap -->
<%@ include file="/WEB-INF/WebServices/wsCorreios.jsp"%>
<form action="/consultorio/clientes" method="POST">
<div class="container-fluid">
<div class="form-group" class="row">
<!-- DADOS PESSOAIS-->
<fieldset class="col-md-4">
<legend>Dados Pessoais</legend>
<div class="form-group">
<label for="nome">Nome completo*</label>
<input type="text" class="form-control" id="nome" name="nome" placeholder="nome" autofocus required>
</div>
<div class="form-group">
<label for="dataNascimento">Data de Nascimento</label>
<input type="date" class="form-control" id="dataNascimento" name="dataNascimento">
</div>
<div class="form-group">
<label for="rg">RG</label>
<input type="text" class="form-control" id="rg" name="rg" placeholder="00.000.000-0">
</div>
<div class="form-group">
<label for="cpf">CPF*</label>
<input type="text" class="form-control" id="cpf" name="cpf" placeholder="000.000.000-00" required>
</div>
<div class="form-group">
<label for="telefone">Telefone*</label>
<input type="tel" class="form-control" id="telefone" name="telefone" placeholder="(00) 0000-0000" required>
</div>
<div class="form-group">
<label for="celular">Celular</label>
<input type="tel" class="form-control" id="celular" name="celular" placeholder="(00) 00000-0000">
</div>
</fieldset>
<!-- ENDEREÇO -->
<fieldset class="col-md-4">
<legend>Endereço</legend>
<div class="form-group">
<label for="cep">CEP</label>
<input type="text" name="cep" value="" size="10" maxlength="9" class="form-control" placeholder="Digite o cep">
</div>
<div class="form-group">
<label for="rua">Rua</label>
<input type="text" name="rua" class="form-control">
</div>
<div class="form-group">
<label for="numero">Número</label>
<input type="text" name="numero" size="5"class="form-control" placeholder="Digite o número">
</div>
<div class="form-group">
<label for="numero">Complemento</label>
<input type="text" name="complemento" size="40" class="form-control" placeholder="Digite o complemento">
</div>
<div class="form-group">
<label for="bairro">Bairro</label>
<input type="text" name="bairro" size="40" class="form-control">
</div>
<div class="form-group">
<label for="cidade">Cidade</label>
<input type="text" name="cidade" size="40" class="form-control">
</div>
<div class="form-group">
<label for="uf">Estado</label>
<input type="text" name="uf" size="2" class="form-control">
</div>
</fieldset>
<!-- DADOS DE LOGIN -->
<fieldset class="col-md-4">
<legend>Dados de login</legend>
<div class="form-group">
<label for="email">E-mail</label>
<div class="input-group">
<span class="input-group-addon">@</span>
<input type="email" class="form-control" name="email" placeholder="email@exemplo.com">
</div>
</div>
<div class="form-group">
<label for="pass">Senha</label>
<input type="password" name="senha" class="form-control" placeholder="senha">
</div>
<div class="form-group">
<label for="passconfirm">Confirme a senha</label>
<input type="password" name="passconfirm" class="form-control" placeholder="confirme sua senha">
</div>
<div class="form-group">
<label for="imagem">Imagem de perfil</label>
<input type="file" name="imagem">
</div>
<br /> <button type="submit" class="btn btn-default">Enviar</button><br />
<input type="reset" value="Limpar" class="btn btn-default"><br />
</fieldset>
</div><!-- fim .form-group -->
</div><!-- fim .container -->
</form>
</tags:pageTemplate>
import java.util.Calendar;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
public class Cliente {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String nome;
@DateTimeFormat
private Calendar dataNascimento;
private String rg;
private String cpf;
private String telefone;
private String celular;
private String email;
private String senha;
//private Endereco endereco;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getNome() {
return nome;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getCpf() {
return cpf;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return this.email;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getTelefone() {
return this.telefone;
}
public Calendar getDataNascimento() {
return dataNascimento;
}
public void setDataNascimento(Calendar dataNascimento) {
this.dataNascimento = dataNascimento;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.nome;
}
}