hello
I've got a problem when i try to insert into my database with hibernate.
I'm a beginner so i try on this small project to understand hibernate.
i use a MYSQL database and this bean
Code:
public class personne
{
private int id;
private String nome;
/** Creates a new instance of Personne */
public personne()
{
}
public personne(String s)
{
this.nom = s;
}
public int getId()
{
return id;
}
public void setId(int i)
{
this.id = i;
}
public void setNom(String n)
{
this.nom = n;
}
public String getNom()
{
return this.nom;
}
}
So, I use the following personne.hbm.xml file :
Code:
<?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="personne" table="personne">
<id name="id" type="int" column="id">
<generator class="native" />
</id>
<property name="nom" type="string" not-null="true" />
</class>
</hibernate-mapping>
And i use this code to insert
Code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
personne p = new personne("maurice");
session.save(p);
session.flush();
tx.commit();
}
catch(Exception e)
{
if(tx != null)
tx.rollback();
System.out.println(e.getMessage());
}
finally
{
sessionFactory.close();
}
When i insert i've an error which tell me that field ID doesn't have a defalut value.
I believe that it was hibernate which generate the id ?
Isn't it ?
Where is the problem ?