在 ejb Web 服务中结束事务性 Web 服务之前如何获取持久化对象的 ID?

how to get persisted object's Id before ending a transactional web service in ejb web services?

我正在开发一个带有 EJB 和 Web 服务的 SOA 应用程序,这些服务被注释为@stateless 和@Webservice。在我的一种 Web 方法中,我的操作从客户端获取 DTO 对象,并在持久化后 returns 它。实际上,我需要 return 在 Web 服务 DTO 响应中将我的对象的持久化 ID 发送给客户端。但是 ejb 在完成操作后提交我的插入。所以在调用 "persist" 之后我还没有自动生成的 Id!

请问有标准解吗?

// force the entityManager to write all the pending changes to the database, and thus generate the ID
entityManager.flush();

// get the ID assigned to the newly persisted entity
Long generatedId = entity.getId();