Delete multiple rows in liferay

650 Views Asked by At

I have a table named xyz with various records

enter image description here

The service.xml has the below mentioned entry

<entity name="xyz" local-service="true" remote-service="true" cache-enabled="true" json-enabled="true">
        <column name="sno" type="int" primary="true" id-type="identity"></column>
        <column name="name" type="String"></column>
        <column name="address" type="String"></column>
        <column name="userid" type="int"></column>

        <finder return-type="Collection" name="userid">
            <finder-column name="userid"></finder-column>
        </finder>
    </entity>

Now I want to delete all the user having userid 10

Using service builder we can do as follow

List<xyz> xyzList = XyzLocalServiceUtil.findbyuserid(10);
if(xyzList!=null && !xyzList.isEmpty()){
    for (xyz xy : xyzList) {
      xyzListLocalServiceUtil.deleteXyz(xy.getSno());
    }
}

But I want to delete all the rows in one go like by executing the below mentioned query

delete from xyz where userid =10;

what would be the liferay service builder equivalent of this?

I am using liferay-6.2-ce-ga3

2

There are 2 best solutions below

2
Daniele Baggio On BEST ANSWER

When you declare a finder in the service.xml, the service builder procedure generates also the delete method based on the same finder.

Search and you will find a method as XyzPersistence.deletebyuserid()

You can build a new method in the XyzLocalServiceImpl to call the delete in persistence layer, ant service-builder, and you'll have the massive delete in the localservice layer.

0
Andre Albert On

Bulk-Delete by a query is not possible (as far as i know). The reason is that liferay usually has(allows) some hooks for removal-of-a-entity callbacks. Also there are some finder-cache involved that might not track those bulk removals properly. You should rather implement a custom bulk deletion method in your *ServiceImpl that handles those bulk deletions (iterate delete) inside one transaction.