In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the mapping way of Hibernate inheritance relationship tree". In daily operation, I believe that many people have doubts about the mapping mode of Hibernate inheritance relationship tree. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the mapping way of Hibernate inheritance relationship tree?" Next, please follow the editor to study!
In the domain model, there can also be inheritance relationships between classes in addition to association and aggregation relationships. There is an one-to-many two-way association between the Companyclass and the Employee class (assuming that employees are not allowed to work part-time in multiple companies at the same time). The employee class is abstract, so it cannot be instantiated, and it has two concrete subclasses: the Hourlyemployee class and the SalariedEmployee class. Because Java only allows a class to have at most one direct parent class, the Employee class, Hourlyemployee class, and SalariedEmployee class form an inheritance tree.
In the object-oriented category, there is also the concept of polymorphism, which is based on inheritance relations. To put it simply, polymorphism means that when a Java application variable is declared as an Employee class, the variable can actually reference either an instance of the HourlyEmployee class or an instance of the SalariedEmployee class. The following code shows polymorphism:
List employees= businessService.findAllEmployees (); Iterator it=employees.iterator (); while (it.hasNext ()) {Employee e = (Employee) it.next (); if (e instanceof HourlyEmployee) {System.out.println (e.getName () + "+ ((HourlyEmployee) e). GetRate ());} else System.out.println (e.getName () +" + ((SalariedEmployee) e). GetSalary ());}
The findAllEmployees () method of the BusinessService class retrieves all Employee objects from the database through Hibernate API. The collection returned by the findAllEmployees () method contains both an instance of the HourlyEmployee class and an instance of the SalariedEmployee class, which is called a polymorphic query. The variable e in the above program is declared to be of type Employee, which may actually reference either an instance of the HourlyEmployee class or an instance of the SalariedEmployee class.
In addition, there is a polymorphic association from the Company class to the employee class because the employees collection of the Company class can contain instances of the HourlyEmployee class and the SalariedEmployee class. There is no polymorphic association from the employee class to the Company class, because the company property of the Employee class only references an instance of the Company class itself. There is no inheritance relationship between database tables, so how to map the inheritance relationship of the domain model to the relational data model?
Each concrete class of the ◆ Hibernate inheritance relationship tree corresponds to a table: the relational data model does not support inheritance relationships and polymorphism in the domain model at all.
The root class of the ◆ Hibernate inheritance relational tree corresponds to a table: unconventional design of the relational data model, adding additional zone element type fields to the database table. In this way, you can make the relational data model support inheritance relationships and polymorphism.
Each class of the ◆ Hibernate inheritance tree corresponds to a table: inheritance relationships are represented by foreign key references in the relational data model.
Tip: a concrete class is a non-abstract class that can be instantiated. The HourlyEmployee class and SalariedEmployee class are concrete classes.
At this point, the study on "what is the mapping method of Hibernate inheritance tree" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.