![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
Hibernate Tutorial 07
Số trang: 8
Loại file: pdf
Dung lượng: 25.10 KB
Lượt xem: 4
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
In our online bookshop application, a customer can place an order for purchasing some books. Ourstaff will process his order and deliver the books to him.
Nội dung trích xuất từ tài liệu:
Hibernate Tutorial 07 Hibernate Tutorial 07 Component Mapping By Gary Mak hibernatetutorials@metaarchit.com September 20061. Using a componentIn our online bookshop application, a customer can place an order for purchasing some books. Ourstaff will process his order and deliver the books to him. The customer can specify differentrecipients and contact details for different day periods (weekdays and holidays). First, we add a newpersistent class Order to our application.public class Order { private Long id; private Book book; private Customer customer; private String weekdayRecipient; private String weekdayPhone; private String weekdayAddress; private String holidayRecipient; private String holidayPhone; private String holidayAddress; // Getters and Setters}Then we create a mapping definition for this persistent class. We just map the properties of thisclass as usual. ... Page 1 of 8One may feel that our Order class is not well designed, since the “recipient”, “phone”, “address”properties have been duplicated two times for weekdays and holidays. From the object-orientedperspective, we should create a class say Contact to encapsulate them.public class Contact { private String recipient; private String phone; private String address; // Getters and Setters}public class Order { ... private Contact weekdayContact; private Contact holidayContact; // Getters and Setters}Now the changes are done for Java. But how can we modify the Hibernate mapping definition toreflect the changes? According to the techniques we have learned before, we can specify Contact asa new persistent class and use a one-to-one association (the simplest way is to use a association with unique=true) to associate Order and Contact. ... Page 2 of 8In this case, modeling the Contact class as a standalone persistent class seems not suitable. This isbecause it is meaningless once departed from an order. Its function is much on grouping somevalues logically. It should not be a complete persistent object with object identifier. Hibernate isproviding a concept called “components” for mapping this kind of objects. ... There is no new persistent object introduced. All the columns mapped for these components are onthe same table as their parent object. Components do not have an identity, and exist only if theirparent does. They are most suitable for grouping several properties as a single object.2. Nested componentsComponents can be even defined to be nested, i.e. components embedded within other components.For example, we can define the phone property as another component and embed it into the contactcomponent.public class Phone { private String areaCode; private String telNo; // Getters and Setters}public class Contact { private String phone; private Phone phone; // Getters and Setters} Page 3 of 8 ... ... 3. References in componentA component can have a reference to its parent object through a mapping.public class Contact { private Order order; private String recipient; private Phone phone; private String address; // Getters and Setters} ... Page 4 of 8A component can be used to group not only normal properties, but also many-to-one and one-to-oneassociations. Suppose we want to associate the address of an order to the address in our customerdatabase.public class Contact { private Order order; private String recipient; private Phone phone; private String address; private Address address; // Getters and Setters} ... ... 4. Collection of componentsSuppose we need to support a more flexible contact mechanism for our book ordering. A customercan specify several contact points for a book delivery as he may not sure which one is most suitablefor a specified time period. Our staff will try to contact these points one by one when they deliverthe books. We use a java.util.Set to hold all the contact points for an order.public class Order { ... private Contact weekdayContact; private Contact holidayContact; private Set contacts; // Getters and Setters}To map many contact points for an order in Hibernate, we can use a collection of components. Weuse to define the components in a collection. For simplicity, we first rollbackour Contact class to the original form. Page 5 of 8public class Contact { private String recipient; private String phone; private String address; // Getters and Setters} ... In additio ...
Nội dung trích xuất từ tài liệu:
Hibernate Tutorial 07 Hibernate Tutorial 07 Component Mapping By Gary Mak hibernatetutorials@metaarchit.com September 20061. Using a componentIn our online bookshop application, a customer can place an order for purchasing some books. Ourstaff will process his order and deliver the books to him. The customer can specify differentrecipients and contact details for different day periods (weekdays and holidays). First, we add a newpersistent class Order to our application.public class Order { private Long id; private Book book; private Customer customer; private String weekdayRecipient; private String weekdayPhone; private String weekdayAddress; private String holidayRecipient; private String holidayPhone; private String holidayAddress; // Getters and Setters}Then we create a mapping definition for this persistent class. We just map the properties of thisclass as usual. ... Page 1 of 8One may feel that our Order class is not well designed, since the “recipient”, “phone”, “address”properties have been duplicated two times for weekdays and holidays. From the object-orientedperspective, we should create a class say Contact to encapsulate them.public class Contact { private String recipient; private String phone; private String address; // Getters and Setters}public class Order { ... private Contact weekdayContact; private Contact holidayContact; // Getters and Setters}Now the changes are done for Java. But how can we modify the Hibernate mapping definition toreflect the changes? According to the techniques we have learned before, we can specify Contact asa new persistent class and use a one-to-one association (the simplest way is to use a association with unique=true) to associate Order and Contact. ... Page 2 of 8In this case, modeling the Contact class as a standalone persistent class seems not suitable. This isbecause it is meaningless once departed from an order. Its function is much on grouping somevalues logically. It should not be a complete persistent object with object identifier. Hibernate isproviding a concept called “components” for mapping this kind of objects. ... There is no new persistent object introduced. All the columns mapped for these components are onthe same table as their parent object. Components do not have an identity, and exist only if theirparent does. They are most suitable for grouping several properties as a single object.2. Nested componentsComponents can be even defined to be nested, i.e. components embedded within other components.For example, we can define the phone property as another component and embed it into the contactcomponent.public class Phone { private String areaCode; private String telNo; // Getters and Setters}public class Contact { private String phone; private Phone phone; // Getters and Setters} Page 3 of 8 ... ... 3. References in componentA component can have a reference to its parent object through a mapping.public class Contact { private Order order; private String recipient; private Phone phone; private String address; // Getters and Setters} ... Page 4 of 8A component can be used to group not only normal properties, but also many-to-one and one-to-oneassociations. Suppose we want to associate the address of an order to the address in our customerdatabase.public class Contact { private Order order; private String recipient; private Phone phone; private String address; private Address address; // Getters and Setters} ... ... 4. Collection of componentsSuppose we need to support a more flexible contact mechanism for our book ordering. A customercan specify several contact points for a book delivery as he may not sure which one is most suitablefor a specified time period. Our staff will try to contact these points one by one when they deliverthe books. We use a java.util.Set to hold all the contact points for an order.public class Order { ... private Contact weekdayContact; private Contact holidayContact; private Set contacts; // Getters and Setters}To map many contact points for an order in Hibernate, we can use a collection of components. Weuse to define the components in a collection. For simplicity, we first rollbackour Contact class to the original form. Page 5 of 8public class Contact { private String recipient; private String phone; private String address; // Getters and Setters} ... In additio ...
Tìm kiếm theo từ khóa liên quan:
Kỹ thuật lập trình Phần cứng Công nghệ thông tin Tin học Quản trị mạngTài liệu liên quan:
-
52 trang 442 1 0
-
24 trang 366 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 332 0 0 -
74 trang 311 0 0
-
96 trang 307 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 300 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 293 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 291 1 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 281 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 280 0 0