I want to copy the record of a table into that same table (nothing but saving the new record with previous record values and here the primary key will auto increment) so how to do it in hibernate(is there any direct method like session.save(Obj)) and I am using Hibernate 4.1.12.
Asked
Active
Viewed 1,748 times
0
-
Clone and save. Or rather use a custom clone method and save the new object. – Thihara Sep 18 '14 at 05:11
4 Answers
0
You can try making all the primary key columns null and then Session.Save(obj).

sacse
- 3,634
- 2
- 15
- 24
-
at that time it is giving exception like Hibernate: “identifier of an instance altered from A to B” – Jagadeesh Sep 18 '14 at 05:04
-
If you have this error, it means the object has not been detached from session and you update the id to null. Try detaching the object first using `evict` method – Joshua H Feb 03 '16 at 10:11
0
Let us Consider:
- Your table name as 'TestTable' with primary key 'id' (key which needs to be auto incremented).
- Your Model Class is 'Test' which has a field 'id' mapped to 'id' column of table 'TestTable'.
You can achieve what you are looking for, using the following approach:
Test test = (Test) sessionFactory.getCurrentSession().createCriteria(Test.class).add(Restrictions.eq("id", id)).uniqueResult();
test.setId(null);
sessionFactory.getCurrentSession().saveOrUpdate(test);

Janakiraman TK
- 56
- 1
-
here also it will give exception like identifier of an instance of (class name) was altered from actual PrimaryKey to null and i think it will not work using saveOrUpdate(obj). – Jagadeesh Sep 18 '14 at 05:27
0
Try this
Test test = (Test) sessionFactory.getCurrentSession().createCriteria(Test.class).add(Restrictions.eq("id", id)).uniqueResult();
Test t=new Test();
t.setName(test.getName());
t.setAge(test.getAge());
sessionFactory.getCurrentSession().save(t);
Or if your table has createddate or modified date fields change that two values and save.
Test test = (Test) sessionFactory.getCurrentSession().createCriteria(Test.class).add(Restrictions.eq("id", id)).uniqueResult();
test.setCreatedDate(new Date());
sessionFactory.getCurrentSession().save(test);

Selva
- 1,620
- 3
- 33
- 63
-
It works but,i thought this will be long procedure if our POJO has more properties. – Jagadeesh Sep 18 '14 at 05:53
-
Can you please give me a example on clone.I am unable to find the example. – Jagadeesh Sep 18 '14 at 07:25
-
-
-
Test test = (Test) sessionFactory.getCurrentSession().createCriteria(Test.class).add(Restrictions.eq("id", id)).uniqueResult(); Test t=test.clone(); t.setName(test.getName()); t.setAge(test.getAge()); sessionFactory.getCurrentSession().save(t); – Selva Sep 23 '14 at 04:05
0
You can try evicting the previous object before saving the new object.
Test test = (Test) sessionFactory.getCurrentSession().createCriteria(Test.class).add(Restrictions.eq("id", id)).uniqueResult();
sessionFactory.getCurrentSession().evict(test);
test.setId(1);
sessionFactory.getCurrentSession().save(test);

Shubham Sharma
- 133
- 2
- 11