Remove entities with hard delete

I view all post about, I think. If I get it in the right way, this below should work:

public class MyBrowse extends AbstractLookup {

@Inject
GroupDatasource myCollectionDs;

@Override
public void init(Map<String, Object> params) {
	getDsContext().addBeforeCommitListener(context -> {
		context.setSoftDeletion(false);
	});
}

but it doesn’t. Seems, at the end, I didn’t got it.

PD To clarify, I want a hardDeletion at this Browse

Hi @apunzi,
If I understood correctly, your entity implements SoftDelete. In “MyBrowse” screen you want to show all the records (even the soft deleted ones). Is that right?

If that is the case, the documentation is quite clear:

On the datasource level – calling CollectionDatasource.setSoftDeletion(false) or setting softDeletion=“false” attribute of collectionDatasource element in the screen’s XML-descriptor.

Using your example:

public class MyBrowse extends AbstractLookup {

@Inject
GroupDatasource myCollectionDs;

@Override
public void init(Map<String, Object> params) {
	myCollectionDs.setSoftDeletion(false);
}

Regards.

Hi @peterson.br,

Thanks for your early reply.

Actually, my goal is to hardDelete records. It’s true, Doc says:

On the datasource level – calling CollectionDatasource.setSoftDeletion(false) or setting softDeletion=“false” attribute of collectionDatasource element in the screen’s XML-descriptor.

for selecting, not for commiting. But @knstvk said in a post, the way to implement hardDeletion for CollectionDatasource is using the beforeCommitListener thing. May be I missunderstood it.

Hi @apunzi,

You’re referring to this comment from @knstvk, right?

But according to this comment, it will not work. I suggest you create a service to handle this entity deletion. You’ll also have to deal with the “related entites” deletion as well (see this link if you have problems deleting the related entites).

To implement the method that will delete a “soft delete” entity, see this answer.

Once the service is complete, you’ll have to decide how to trigger the deletion of the entity. You may use an EntityListener (BeforeDelete), if you always want to trigger this “hard delete” feature. Or create a custom action, if you only want to trigger from a specific screen.

P.S.: If your entity is simple (no related entities problems on delete) and you want to delete from a specific screen, it’ll be easier to just create a custom action and use the solution that @knstvk suggested.

Regards.

1 Like

Thanks a lot for the info.

No worry about related entities, because in this screen, if there are any I want an exception to be thrown. The goal is to be able to delete an entity only if there are no related to it. I didn’t find a way to check it using the softDeletion=true. I haven’t found a way to verify that, unless to check all the entities in which it could be referenced (and there are so many). So, with softDeletion=false I could abort the process when the FKs exception is thrown. But according your info, I can’t.

I could create a Service using SQL delete sentence and create a custom button at this browse, but first I was looking for a cleaner solution.