tailieunhanh - Java Persistence with Hibernate 2nd phần 7
theo dõi và lưu lại những thay đổi bạn thực hiện một đối tượng ở trạng thái liên tục. Ngay sau khi bạn đóng phiên, ví dụ được xem là tách ra. Làm cho một đối tượng liên tục thoáng qua Bạn có thể dễ dàng thực hiện một đối tượng liên tục thoáng qua, loại bỏ tình trạng kéo dài của nó từ cơ sở dữ liệu, với các phương pháp xóa () (xem danh sách 9,5). | 492 CHAPTER 11 Implementing conversations Session session getSessionFactory .openSession First step in the conversation Item item Item new Long 1 .commit Second step in the conversation Item newItem new Item newItem Roll back the conversation The persist method can delay inserts because it doesn t have to return an identifier value. Note that the newItem entity is in persistent state after you call persist but it has no identifier value assigned if you map the persistent class with an identity or select generator strategy. The identifier value is assigned to the instance when the INSERT occurs at flush-time. No SQL statement is executed when you call persist outside of a transaction. The newItem object has only been queued for insertion. Keep in mind that the problem we ve discussed depends on the selected identifier generator strategy you may not run into it or you may be able to avoid it. The nontransactional behavior of persist will be important again later in this chapter when you write conversations with JPA and not Hibernate interfaces. Let s first complete the implementation of a conversation with an extended Session. With a session-per-conversation strategy you no longer have to detach and reattach or merge objects manually in your code. You must implement infrastructure code that can reuse the same Session for a whole conversation. Manaếinế the current Session The current Session support we discussed earlier is a switchable mechanism. You ve already seen two possible internal strategies One was thread-bound and the other bound the current Session to the JTA transaction. Both however closed the Session at the end of the transaction. You need a different scope of the Session for the session-per-conversation pattern but you still want to be able to access the current Session in your application code. A third built-in .
đang nạp các trang xem trước