Search code examples
web-servicesjpaejbjtastateless

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


I am developing an SOA application with EJB and web services that are annotated as @stateless and @Webservice. In one of my web methods, my action gets a DTO object from client and returns it after persisting it. Actually I need to return the persisted Id of my object to the client in the web service DTO response. But ejb commits my insertion after finishing the action. So after calling "persist" I don't have the auto generated Id yet !

Any standard solution please?


Solution

  • // 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();