I am new to Hibernate. When I save a specific object, it overwrites the data from the existing one.
I used the ID as automatically generated, as shown below:
@Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") private int id;
Here I save the object as shown below:
class StudDAO() { public static void main(String[] args){ StudDAO obj = new StudDAO(); Stud stud = new Stud(); stud.setName("Test"); stud.setCity("Mumbai"); obj.createStud(stud); } public void createStud(Stud stud) { try { Session session = HibernateSessionFactory.getSessionFactory().openSession(); Transaction transaction = session.beginTransaction(); session.save(stud); transaction.commit(); } catch (Exception e) {
}
if I change the value of the entity next time, then it should generate the next identifier, and not start with the 1st id.
any time result will be the same as
mysql> select * from stud; +----+--------+------+ | id | city | name | +----+--------+------+ | 1 | Mumbai | Test | +----+--------+------+
1 row per set (0.00 s)
what i want is as follows:
mysql> select * from stud; +----+--------+------+ | id | city | name | +----+--------+------+ | 1 | Mumbai | Test | | 2 | Mumbai | Test | +----+--------+------+ 2 rows in set (0.00 sec)
Please help me for the same.
hibernate
Sweety
source share