Automatically detach entity when using a streaming query

Viewed 163

I have an interface method in my repository

@Query("from Alert a")
Stream<Alert> streamAll();

Since I may have a lot of alerts, but I only need to process each one a record at a time and I don't need the associated data afterwards, I write code as follows

streamAll()
  .forEach(alert-> {
    doProcessing(alert);
    entityManager.detach(alert);
  });

Where I explicitly do a detach of the alert. I was wondering is there a nicer way of doing this so I can simply do

streamAllWithDetach()
  .forEach(this::doProcessing);

Without writing a lot of custom code or AOP wrappers.

1 Answers

just keep it simple. let's create a method to wrap

doProcessing(alert);
entityManager.detach(alert);
Related