CHAPTER 5 Inheritance and custom types If your superclass is concrete, then an additional table is needed to hold instances of that class. We have to emphasize again that there is still no relationship between the database tables, except for the fact that they share some similar columns. The advantages of this mapping strategy are clearer if we examine polymorphic queries. For example, a query for BillingDetails executes the following SQL statement: select BILLING_DETAILS_ID, OWNER, NUMBER, EXP_MONTH, EXP_YEAR, ACCOUNT, BANKNAME, SWIFT CLAZZ_ from ( select BILLING_DETAILS_ID, OWNER, NUMBER, EXP_MONTH, EXP_YEAR, null as ACCOUNT, null as BANKNAME, null as SWIFT, 1 as CLAZZ_ from CREDIT_CARD union select BILLING_DETAILS_ID, OWNER, null as NUMBER, null as EXP_MONTH, null as EXP_YEAR, … ACCOUNT, BANKNAME, SWIFT, 2 as CLAZZ_ from BANK_ACCOUNT ) This SELECT uses a FROM-clause subquery to retrieve all instances of BillingDetails from all concrete class tables. The tables are combined with a UNION operator, and a literal (in this case, 1 and 2) is inserted into the intermediate result; Hibernate reads this to instantiate the correct class given the data from a particular row. A union requires that the queries that are combined project over the same columns; hence, we have to pad and fill up nonexistent columns with NULL. You may ask whether this query will really perform better than two separate statements. Here we can let the database optimizer find the best execution plan to combine rows from several tables, instead of merging two result sets in memory as Hibernate s polymorphic loader engine would do.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.