Beginner |
|
Joined: Sun Nov 19, 2006 6:18 am Posts: 28
|
Hi,
Following iss my code snippet. I m trying to add 2 SmhostBean objects with id 1 and id 2. The id is the primary key here.
I m adding them in the same transaction.
But id 1 already exists in the Db.si its insertion the second time should throw an exception whithc it does. But what i obsever is that SmhostBean object with id 2 gets inserted.The roll back is not working.
Kindly help ...
Session session=HibernateUtil.currentSession();
Transaction transaction=session.beginTransaction();
SmHostBean hostBean=new SmHostBean();
hostBean.setId(2);
hostBean.setIpaddress("22");
hostBean.setPort("22");
session.save(hostBean);
hostBean=new SmHostBean();
hostBean.setId(1); //this id already exists in the DB
hostBean.setIpaddress("11");
hostBean.setPort("11");
session.save(hostBean);
try{
session.flush();
transaction.commit();
}catch (Exception e) {
transaction.rollback();
}
|
|