Witam
Proboje zapisac cos do bazy danych za pomoca hibernate i pojawia sie blad:
org.hibernate.exception.GenericJDBCException: could not insert: [model.Employee]
gdy robię odczyt z bazy i usuniecie z bazy wszystko działa poprawnie
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans;
import hib.HibernateUtil;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import model.Employee;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import javax.faces.context.FacesContext;
/**
*
* @author marezo
*/
@ManagedBean
@RequestScoped
public class JSFManagedBean {
private String n;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getN() {
return n;
}
public void setN(String n) {
this.n = n;
}
/** Creates a new instance of JSFManagedBean */
public JSFManagedBean() {
}
public void addProdukt(int id, String n) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Employee p = new Employee();
p.setId(id);
p.setName(n);
s.save(p);
tx.commit();
s.close();
}
public String dodaj()
{
addProdukt(id, n);
n = null;
id = 0;
return null;
}
public List<Employee> getProdukty() {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session s = sf.openSession();
List<Employee> pp =(List<Employee>)s.createQuery("SELECT e FROM Employee e").list();
s.close();
return pp;
}
public void deleteProdukt(Integer pid) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Employee p = (Employee) s.get(Employee.class, pid);
s.delete(p);
tx.commit();
s.close();
}
public String usun() {
FacesContext context = FacesContext.getCurrentInstance();
String spid = context.getExternalContext().getRequestParameterMap().get("pid");
Integer pid = Integer.parseInt(spid);
deleteProdukt(pid);
return null;
}
}
.
Klasa /*
To change this template, choose Tools | Templates</li>
and open the template in the editor.
*/</li>
</ul>
package model;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
*
@Author marezo
*/
@Entity
@Table(name = "employee")
@NamedQueries({
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")})
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
public Employee() {
}
public Employee(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
</CODE>
mapowANIE:
</li>
</ul>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.Employee" table="employee">
<id name="id" type="int" column="id">
<generator class="native" />
</id>
<property name="name" column="name" type="string" not-null="true" />
</class>
</hibernate-mapping>
Czy ktoś wie może co zrobiłem nie tak?