Java Persistence with Hibernate phần 5

Để chứng minh sự linh hoạt của các loại bản đồ tùy chỉnh, bạn không thay đổi lớp MonetaryAmount (và liên tục các lớp học khác) tại tất cả các bạn thay đổi chỉ có các loại bản đồ tùy chỉnh, như thể hiện trong danh sách 5,5. | Polymorphic associations 315 the given instance is this would require a database hit which you try to avoid with lazy loading in the first place. To perform a proxy-safe typecast use load User user User userid BillingDetails bd Narrow the proxy to the subclass doesn t hit the database Creditcard cc Creditcard expiryDate After the call to load bd and cc refer to two different proxy instances which both delegate to the same underlying CreditCard instance. However the second proxy has a different interface and you can call methods like getExpiryDate that apply only to this interface. Note that you can avoid these issues by avoiding lazy fetching as in the following code using an eager fetch query User user User .add id uid .setFetchMode defaultBillingDetails .uniqueResult The users defaultBillingDetails have been fetched eagerly CreditCard cc CreditCard expiryDate Truly object-oriented code shouldn t use instanceof or numerous typecasts. If you find yourself running into problems with proxies you should question your design asking whether there is a more polymorphic approach. Hibernate also offers bytecode instrumentation as an alternative to lazy loading through proxies we ll get back to fetching strategies in chapter 13 section Defining the global fetch plan. One-to-one associations are handled the same way. What about many-valued associations for example the collection of billingDetails for each User Polymorphic collections A User may have references to many BillingDetails not only a single default one of the many is the default . You map this with a bidirectional one-to-many association. In BillingDetails you have the following many-to-one name user class User column USER_ID 316 CHAPTER 7 Advanced entity association mappings In the .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
29    107    2    01-07-2024
Đã 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.