Olá pessoal,
o que será que está acontecendo ao tentar gravar me da essa mensagem :
could not insert: [br.com.wincomp.projeto.bean.Alunos]
Se alguém puder me ajudar agradeceria …
abs
Paulo
–web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>cadastra</servlet-name>
<servlet-class>br.com.wincomp.projeto.servlet.IncluirAlunos</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cadastra</servlet-name>
<url-pattern>/cadastra</url-pattern>
</servlet-mapping>
</web-app>
package br.com.wincomp.projeto.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import br.com.wincomp.projeto.bean.Alunos;
public class IncluirAlunos extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
PrintWriter output = response.getWriter();
output.println("<html>");
output.println("<body>");
try {
String nome = request.getParameter("nome");
String endereco = request.getParameter("endereco");
String cep = request.getParameter("cep");
String cidade = request.getParameter("cidade");
String estado = request.getParameter("estado");
String pais = request.getParameter("pais");
Alunos aluno = new Alunos();
aluno.setNome(nome);
aluno.setEndereco(endereco);
aluno.setCep(cep);
aluno.setCidade(cidade);
aluno.setEstado(estado);
aluno.setPais(pais);
//
SessionFactory sf;
sf = new Configuration().configure("br/com/wincomp/projeto/bean/hibernate.cfg.xml").buildSessionFactory();
//sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession(); // abre session
Transaction tx = s.beginTransaction(); //cria transação
s.save(aluno);
//s.saveOrUpdate(aluno);
tx.commit();
s.close();
//
output.println("<font color=blue>");
output.println("Operação Relalizada com Sucesso ....");
output.println("</font>");
}
catch (Exception ex) {
output.println(ex.getMessage());
}
output.println("</body>");
output.println("</html>");
output.close();
}
}
–Alunos.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping.dtd">
<hibernate-mapping>
<class name="br.com.wincomp.projeto.bean.Alunos" table="Alunos">
<id name="idaluno" column="idaluno" type="int">
<generator class="native"/>
</id>
<property name="nome" column="nome" type="string"/>
<property name="endereco" column="endereco" type="string"/>
<property name="cep" column="cep" type="string" />
<property name="cidade" column="cidade" type="string"/>
<property name="estado"/>
<property name="pais" column="pais" type="string"/>
</class>
</hibernate-mapping>
–hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/loja</property>
<property name="connection.username">root</property>
<property name="connection.password">paribe</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">false</property>
<mapping resource ="br/com/wincomp/projeto/bean/Alunos.hbm.xml"/>
</session-factory>
</hibernate-configuration>
– index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
function escolherAction(acao) {
if (acao == "cadastrar")
document.produtoForm.action = "cadastra";
else if (acao == "consultar")
document.produtoForm.action = "consulta";
else
document.produtoForm.action = "remove";
document.produtoForm.submit();
}
</script>
</head>
<body>
<form name="produtoForm" method="get">
<table>
<tr>
<td>Nome:</td>
<td colspan="2">
<input type="text" name="nome">
</td>
</tr>
<tr>
<td>Endereço:</td>
<td colspan="2">
<input type="text" name="endereco">
</td>
</tr>
<tr>
<td>Cep:</td>
<td colspan="2">
<input type="text" name="endereco">
</td>
</tr>
<tr>
<td>Cidade:</td>
<td colspan="2">
<input type="text" name="cidade">
</td>
</tr>
<tr>
<td>Estado:</td>
<td colspan="2">
<input type="text" name="estado">
</td>
</tr>
<tr>
<td>Pais:</td>
<td colspan="2">
<input type="text" name="pais">
</td>
</tr>
<tr>
<td>
<input type="button" name="cadastrar" value="Cadastrar" onclick="escolherAction('cadastrar')">
</td>
</tr>
</table>
</form>
</body>
</html>