0

I have one class with specific columns, say

Class A
{
    private String A;
    private String B;
    private String C;

    // Getter Setter of respectives
}

Now what happened I have same value of column A and column B only column C's value change. So I do something like below

A a = new A();
a.setA(..);
a.setB(..);

for(i=0;i<length;i++){
     a.setC(..);

     getHibernateTemplate.saveOrUpdate(a);

     // or something like this
     // A a1 = new A();
     // a1 = a;
     // a1. setC(..);
     // getHibernateTemplate.saveOrUpdate(a1);

}

My issue is it does not store length number of records, it only updates that single record.

I know the reason that hibernate access it as persistent object and even if I change value and again save it will update existing record and it can be resolve by taking new object every time and setting it all values. But I don't want it, is there any way to tell hibernate to save that record instead of updating?

2 Answers 2

2

You haven't described actual entity details. If you want to save entity with the same values, set the identifier property as null.

From Documentation -

saveOrUpdate()

  • if the object is already persistent in this session, do nothing
  • if another object associated with the session has the same identifier, throw an exception
  • if the object has no identifier property, save() it
  • if the object's identifier has the value assigned to a newly instantiated object, save() it
  • if the object is versioned by a or , and the version property value is the same value assigned to a newly instantiated object, save() it
  • otherwise update() the object

saveOrUpdateAll()

  • Save or update all given persistent instances, according to its id (matching the configured "unsaved-value"?). Associates the instances with the current Hibernate Session.

[If it works, can try this for your other query]


Edit : It's mine oversight, I haven't checked your code carefully. You have defined object A outside for loop, therefore the same object was being updated in each iteration. Try the below code, might help.

for(i=0;i<length;i++){
A a = new A();  //-- Create new object for each iteration
a.setA(..);
a.setB(..);
a.setC(..);

getHibernateTemplate.saveOrUpdate(a);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I had tried by setting identifier value with null, but it does not work.
That will work, but I dont want to set setA() and setB() again that's my concern.
@commit For that, you can explicitly define method in entity to return a new object having default values or appropriate constructor or can clone() & then can set only required field to be altered.
1

Yes.Try save(a) instead of saveOrUpdate(a)

getHibernateTemplate.save(a);  //each time a new object saves.

1 Comment

Thanks, I have upvoted for your solution but this will not work for me, this is only consept of my code because whole code I can't explain here however in my code I am using method for saving whole collection by saveOrUpdateAll() and hibernateTemplate class has not saveAll() method, In that case what should I do? I have thought about this before and I have asked related this question here also stackoverflow.com/questions/17568898/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.