Issue
I have a search.jsp
page where I search & retrieve a record from Database. I have to duplicate this record and make modifications and save both records.
Ex:
method1(){
Response response=new Response();
//To perform DB search
response=delegate.requestSearch(//some request criteria);
Types obj1=(Types) response.getBaseEntity();
Types obj2=obj1;
obj2.setPromaryKeyObject(//some unique value);
}
In my DAO, save method:
session.save(obj1);
session.save(obj2);
transaction.commit(); // transaction
When the debug point commits transaction, I get one update query only. But I expect two insert queries.
But this works fine. This was an attempt but this is not what I want to do.
Types tempObj1 = new Types();
tempObj1.setValue1(obj1.getValue1();
// all values of the object should be set
session.save(tempObj1);
Types tempObj1 = new Types();
tempObj2.setValue1(obj2.getValue1();
// all values of the object should be set
session.save(tempObj2);
transaction.commit(); // transaction
In this case I trigger two insert queries.
I'm pretty sure that the mistake is in my hibernate method as I'm very new to Hibernate. This same code works fine when I was using JDBC and MySql queries.
Solution
This is due the obj2 == obj1
. Try to obj2 = obj1.clone()
;
Answered By - Roman C
Answer Checked By - Cary Denson (JavaFixing Admin)