2015-04-12 75 views
0

我正在開發一個帶有註釋爲@stateless和@Webservice的EJB和Web服務的SOA應用程序。在我的一個Web方法中,我的操作從客戶端獲取DTO對象,並在持久化之後返回它。實際上,我需要在Web服務DTO響應中將我的對象的持久標識返回給客戶端。但ejb在完成操作後提交我的插入。所以在調用「堅持」後,我沒有自動生成的ID呢!如何在ejb Web服務中結束事務性Web服務之前獲取持久化對象的Id?

任何標準的解決方案,請嗎?

回答

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(); 
+0

在webservice會話bean中注入entityManager真的很標準嗎?我只是將它注入一個通用的抽象dao bean中,它處理我所有的crud操作。 –

+1

然後向DAO添加一個flush方法,並從web服務調用dao.flush()。 –

+0

這似乎是一個很好的建議。謝謝 :) –