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

时间:2015-04-12 12:39:13

标签: web-services jpa ejb jta stateless

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

请问任何标准解决方案吗?

1 个答案:

答案 0 :(得分:1)

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