I have a REST API that has synchronous endpoints, which means:

a request comes in code is executed the result is written to a postgresDB using hibernate (with hikariCP) result is returned in a http response

This can take a long time, so I wanted to make the REST API asynchronous instead:

a POST request comes in the request is written to the DB an OK response is returned my code is executed and writes the result to the DB a GET request comes in to fetch the result for the if the result for this request is in the DB, return it in a http response

I started out by doing this in a naive way, by just starting a new thread for my code (and returning an OK response), which also tries to write the result to the DB, but I am getting a failed to lazily initialize a collection of role: online.ecm.API.model.Document.requestResponses, could not initialize proxy - no Session in that thread and I have read that you should avoid messing with hibernate.current_session_context_class.

I had difficulty to understand how to add the DB writes when I looked at different examples for "spring async REST API" and I was not sure which of the ways that I saw is still relevant and best suited for my case. Can anybody point me to an example or write a sample of such a async REST API with a database in spring? I was unable to find any fitting or clear answer for this problem, even though it seems like a common architecture to me.