TAILIEUCHUNG - Java Persistence with Hibernate 2nd phần 6

Đầu tiên, bạn lấy các đối tượng từ cơ sở dữ liệu với các định danh được. Bạn chỉnh sửa các đối tượng, và những thay đổi này là tuyên truyền cơ sở dữ liệu trong tuôn ra khi () được gọi là. Cơ chế này được gọi là tự động bẩn kiểm tra điều đó có nghĩa là Hibernate Tất cả các chiến lược lấy trước đó là hữu ích nếu bạn cố gắng | 406 CHAPTER 9 Working with objects Session Persistence Context 1 2. 1 1 A get id 1 K Y 1 1. 3. f 4. 1 5. 1 openSessionQ setValue v flush close Figure Modifying a persistent instance First you retrieve the object from the database with the given identifier. You modify the object and these modifications are propagated to the database during flush when is called. This mechanism is called automatic dirty checking that means Hibernate tracks and saves the changes you make to an object in persistent state. As soon as you close the Session the instance is considered detached. Making a persistent object transient You can easily make a persistent object transient removing its persistent state from the database with the delete method see listing . Listing Making a persistent object transient using delete Session session Transaction tx Item item Item new Long 1234 item Look at figure . The item object is in removed state after you call delete you shouldn t continue working with it and in most cases you should make sure any reference to it Session Persistence Context 1 2. f- -4- load id - p 1 K 3. Y delete o - I J R -I L I . - T I 1 1. I 4. I 5. openSessionO flushO close Figure Making a persistent object transient The Hibernate interfaces 407 in your application is removed. The SQL DELETE is executed only when the Session s persistence context is synchronized with the database at the end of the unit of work. After the Session is closed the item object is considered an ordinary transient instance. The transient instance is destroyed by the garbage collector if it s no longer referenced by any other object. Both the in-memory object instance and the persistent database row will have been removed. FAQ Do I have to load an object to delete it Yes an object has to be loaded into the persistence context an instance has to be in persistent .

TỪ KHÓA LIÊN QUAN
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.