TAILIEUCHUNG - Hibernate Tutorial 03

In the previous example, we use ISBN as the identifier of a book object. This ID is assigned manually, so it is called an “assigned identifier”. | Hibernate Tutorial 03 Object Identifier By Gary Mak hibernatetutorials@ September 2006 1. Auto-generated object ID In the previous example we use ISBN as the identifier of a book object. This ID is assigned manually so it is called an assigned identifier . . Providing a generated ID for persistent object Suppose that one user mistyped the ISBN for a book and saved it to database. He found out this mistake later and wanted to correct it. But is it able to do so in Hibernate For a persistent object the ID cannot be modified once assigned. Hibernate will treat two objects with different IDs as two totally different objects. One possible way for changing this ID is to touch the database. In our relational model BOOK table has a primary key column ISBN and CHAPTER table has a foreign key column BOOK_ISBN referencing the BOOK table. Both columns need to be changed to correct a single mistake. We can see that it is a bad practice of using something with business meaning as the object ID especially those inputted by users. In opposite we should use something that will be generated automatically as the object ID. Since this ID has no business meaning at all it will never be changed. For the first step of implementing this idea we modify our Book class to add an id property which will be generated automatically public class Book private long id private String isbn private String name private Publisher publisher private Date publishDate private int price private List chapters Getters and Setters Page 1 of 8 . ID generation in Hibernate Next we need to ask Hibernate to generate this ID for us before persisting to the database. Hibernate is providing many built-in strategies for ID generation. Some of them are only available for specified databases. The most typical way of generating such an ID is to use an auto-incremented sequence number. For some kinds of databases including HSQLDB we can use a sequence generator to generate this sequence number. This .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.