Message Boards

Which is best way update the record in liferay?

Montej Shah, modified 7 Years ago.

Which is best way update the record in liferay?

Junior Member Posts: 48 Join Date: 2/18/15 Recent Posts
Hi All,
I have created the Table named Subject. Which contain the below field.
subjectPrimaryKey
subjectName
subjectValue
countryInformationId

Now I have scenario where I need to update the 100 of records in the bunch(also add 2 or 3 new subject) where the countryInformationId is same.

I have two solution for that.
First One
//remove all the 100 records.
SubjectUtil.removeByCountryInformationId(countryInformationId);
//add the 100 records as new ( which are need to update) and also other new records.
for(...){
subjectLocalService
.createSubject(CounterLocalServiceUtil
.increment(Subject.class.getName()));
..
..
subjectLocalService.update(subject);
}


Second One
//iterate over all the sujbect
for(...){
//Get individual record by the subject id
subjectLocalService.getSubject(subjectId);
//Update record and save it.
....
subjectLocalService.update(subject);
}
//add new records which are not exist in database.
for(...){
subjectLocalService
.createSubject(CounterLocalServiceUtil
.increment(Subject.class.getName()));
..
..
subjectLocalService.update(subject);
}




Which is best solution? and Why? If you have any better solution then you can also refer it to me.
Ricardo Vieira, modified 7 Years ago.

RE: Which is best way update the record in liferay?

Junior Member Posts: 54 Join Date: 8/16/10 Recent Posts
In my opinion you should query the db for the 100 records, update them and save all in batch, if anything goes wrong, just rollback, or do it in batches, in case someday there will be more then 100, like 20 at the time, just need to work on the query.

and then create the new ones.

If you remove the 100 and create them again, they won't be the same records, they will have new id's, so that's not a good solution in my view.
thumbnail
Andrew Jardine, modified 7 Years ago.

RE: Which is best way update the record in liferay?

Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts
Is this a one time operation? Is it is then I would just go with number two... It'll take less time to run that it would to find a "optimised solution". If you are going to be doing 100 record updates regularly though, I would donasnhas been suggested -- try to do batches of 10 - 20 ... That way if one or two fail, it won't affect the entire set of records.