Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3
Mapping documents: Vehicle.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ranlogic.prueba.Vehicle" table="vehiculos">
<id name="id" column="veh_id">
<generator class="identity"/>
</id>
<property name="name" column="veh_marca"/>
<property name="price" column="veh_valor"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Configuration cfg = new Configuration();
cfg.addClass(obj.getClass());
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(obj);
session.getTransaction().commit();
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Hibernate: insert into vehiculos (veh_marca, veh_valor) values (?, ?)
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html
Well. My problem is that every time I save an object (Vehicle in this case) Hibernate UPDATES the tabla instead of inserting a new record.
I have copied a singleton class called HibernateUtil, with a method getSessionFactory.
In the main method I wrote this:
Code:
Vehicle veh = new Vehicle();
veh.setId((long)5);
veh.setName("Vehicle 3");
veh.setPrice(25000.0);
HibernateTest.save(veh);
The save method:
Code:
public static void save(Object obj){
configurate(obj);
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(obj);
session.getTransaction().commit();
}
And, finally, the configurate method:
Code:
private static void configurate(Object obj){
Configuration cfg = new Configuration();
cfg.addClass(obj.getClass());
}
The veh_id field is identity in MySQL (PK, not null and autoincrement).
I don't know why Hibernate performs an Update instead an Insert.
By the way, also deletes all previous records