Database object retrieval

As in Figure 937, “Retrieving hibintro.v1.model.User instances by HQL. ” objects being stored by Figure 944, “Inserting payment information ” may be queried using HQL.

Java
package inherit.v1;
	      ...
	      public class RetrieveCredit {
	      public static void main(String[] args) {
	      ...
	      final Transaction transaction = session.beginTransaction();

	      final Query searchCreditPayments = session.createQuery("from inherit.v1.CreditCard"); ❶
	      final List<CreditCard> creditCardList = (List<CreditCard>) searchCreditPayments.list();
	      for (final CreditCard c: creditCardList) {
	      System.out.println(c);
	      } ...
Sql
INFO: HHH000232: Schema update complete
	      Hibernate: 
	      select
              creditcard0_.id as id0_,
              creditcard0_.created as created0_,
              creditcard0_.number as number0_,
              creditcard0_.cardType as cardType0_,
              creditcard0_.expiration as expiration0_ 
	      from
              BillingDetails creditcard0_ 
	      where
              creditcard0_.dataType ❶='Credit card'

	      CreditCard: number=4412 8334 4512 9416, created 2013-02-19 13:09:22.0,
              cardType=1, expiration=2015-05-18 00:00:00. 

Some Remarks: Our query asks for instances of inherit.v2.CreditCard ❶. This gets implemented as an SQL SELECT choosing datasets whose discriminator attribute value of dataType ❶ equals Credit card. The current result set contains just one element ❷ in accordance with Figure 944, “Inserting payment information ”.

Retrieving both inherit.v1.CreditCard and inherit.v1.BankAccount instances is accomplished by querying for the common base class inherit.v1.BillingDetails:

Java
package inherit.v1;
	      ...
	      public class RetrieveAll {
	      ...
	      final Query searchBilling = session.createQuery("from inherit.v1.BillingDetails");
	      @SuppressWarnings("unchecked")
	      final List<BillingDetails> billingDetailsList = (List<BillingDetails>) searchBilling.list();
	      for (final BillingDetails c: billingDetailsList) {
	      System.out.println(c);
	      } ...
Sql
INFO: HHH000232: Schema update complete
	      Hibernate: 
	      select
              billingdet0_.id as id0_,
              ...
              billingdet0_.dataType as dataType0_ 
	      from
              BillingDetails billingdet0_

	      CreditCard: number=4412 8334 4512 9416, created 2013-02-19 13:09:22.0, ❶
              cardType=1, expiration=2015-05-18 00:00:00.0
	      BankAccount: number=1107 2 31, created 2013-02-19 13:09:22.0,
              bankName=Lehman Brothers, swiftcode=BARCGB22

This is the first example of a polymorphic query yielding a heterogeneous result set❶.