The key codes are bellow. I run these codes under Eclipse many times, but only one record is added to DB.
It seems that the new record is added into DB when first time running. Other times, it only update the same record in DB.
But the sql showed by Hibernate is always "insert into department values(?,?,?)". What happened? How to add one new row every time?
POJO:
Code:
public class Department
{
private int deptID;
private String deptName;
private int manager;
private String address;
//getters and setters........
}
HBM XML file:
Code:
<hibernate-mapping package="com.tt.orm">
<class name="com.tt.orm.Department" table="department">
<id name="deptID" column="DeptID" >
<generator class="increment"/>
</id>
<property name="deptName"/>
<property name="manager"/>
<property name="address"/>
</class>
</hibernate-mapping>
Test Code:
Code:
public static void main(String[] args)
{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Department dept = new Department();
//dept.setDeptID(3);
dept.setAddress("test1 address");
dept.setDeptName("HR1-1");
session.save(dept);
session.getTransaction().commit();
session.close();
}
I'm a beginner for Hibernate. Thank you. Any help is welcome.