tailieunhanh - Java Persistence with Hibernate 2nd phần 4
như một hộp đen và bỏ qua hành vi này. Tuy nhiên, khi bạn đang xử lý sự cố một vấn đề khó khăn, bạn cần để có thể xem chính xác những gì đang xảy ra bên trong Hibernate. Bởi vì Hibernate là mã nguồn mở, bạn Nếu bạn thao tác các bộ sưu tập, liên kết, phía noninverse được sử dụng để cập nhật các cột user_id | 232 CHAPTER 5 Inheritance and custom types We left out the usual mandatory housekeeping methods in this example. The important additional method is setParameterValues of the Parameterized-Type interface. Hibernate calls this method on startup to initialize this class with a convertTo parameter. The nullSafeSet methods uses this setting to convert to the target currency when saving a MonetaryAmount. The nullSafeGet method takes the currency that is present in the database and leaves it to the client to deal with the currency of a loaded MonetaryAmount this asymmetric implementation isn t the best idea naturally . You now have to set the configuration parameters in your mapping file when you apply the custom mapping type. A simple solution is the nested type mapping on a property property name initialPrice column name INITIAL_PRICE column name INITIAL_PRICE_CUR type name param name convertTo USD param type property However this is inconvenient and requires duplication if you have many monetary amounts in your domain model. A better strategy uses a separate definition of the type including all parameters under a unique name that you can then reuse across all your mappings. You do this with a separate typedef an element you can also use it without parameters typedef class name monetary_amount_usd param name convertTo USD param typedef typedef class name monetary_amount_eur param name convertTo EUR param typedef What we show here is a binding of a custom mapping type with some arguments to the names monetary_amount_usd and monetary_amount_eur. This definition can be placed anywhere in your mapping files it s a child element of hibernate-mapping as mentioned earlier in the book larger applications have often one or several files with no class mappings . With Hibernate extensions you can define named custom types with parameters
đang nạp các trang xem trước