【问题标题】:how to get persisted object's Id before ending a transactional web service in ejb web services?如何在结束 ejb Web 服务中的事务性 Web 服务之前获取持久对象的 ID?
【发布时间】:2015-06-17 19:59:14
【问题描述】:

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

请问有什么标准解决方案吗?

【问题讨论】:

    标签: web-services jpa ejb jta stateless


    【解决方案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();
    

    【讨论】:

    • 在 web 服务会话 bean 中注入 entityManager 真的很标准吗?我只将它注入到一个通用的抽象 dao bean 中,它处理我所有的 crud 操作。
    • 然后在DAO中添加flush方法,并从web服务调用dao.flush()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2011-06-12
    相关资源
    最近更新 更多