How to write dynamic delete query in liferay that will delete selected values from a row of data in a table

993 views Asked by At

I want to write a dynamic query which will delete values from a row those will be supplied, i.e a row contains lets say 6 elements i want to delete only 3 of them.How i can achieve that using liferay dynamic query.I am creating a book database and and this my table,I have added picture here. enter image description here

Now i want to delete description and authorname from second row lets say.How i can do that???Previously i was doing by this

long bookId = ParamUtil.getLong(actionRequest, "bookId");
BookLocalServiceUtil.deleteBook(bookId);
SessionMessages.add(actionRequest, "deleted-book"); 
_log.info("#################Book Deleted Successfully#########################");

Lets assume that bookName,description etc i can supply inside the delete method.

1

There are 1 answers

0
Thibault Martin On

The only and best way to perform what you want to is to update your entity. For doing so, you have to first retrieve the entity using a DynamicQuery and then to update it using the persistence layer.

If you have the bookId and it is your only criterion, you can also use the persistence layer directly to retrieve a bean of your entity.

For your purpose, I think the following is a good solution, in your BookLocalServiceImpl:

public void updateDescription(long bookId, String description) throws SystemException {
    Book updateMe = bookPersistence.fetchByPrimaryKey(bookId);
    updateMe.setDescription(description);
    bookPersistence.update(updateMe);
}

Then in your controller simply call BookLocalServiceUtil.updateDescription("") and voila, you just cleared the description.