Hibernate - Polymorphism
HQL and JPQL queries are inherently polymorphic.
List<Payment> payments = entityManager.createQuery(
"select p " +
"from Payment p ", Payment.class )
.getResultList();
This query names the Payment entity explicitly. However, all subclasses of Payment are also available to the query. So, if the CreditCardPayment and WireTransferPayment entities extend the Payment class, all three types would be available to the entity query, and the query would return instances of all three.
This behavior can be altered in two ways:
by limiting the query to select only from the subclass entity
by using either the org.hibernate.annotations.Polymorphism annotation (global, and Hibernate-specific). See the @Polymorphism section for more info about this use case.
The HQL query from java.lang.Object is totally valid (although not very practical from a performance perspective)!
It returns every object of every entity type defined by your application mappings.
List<Payment> payments = entityManager.createQuery(
"select p " +
"from Payment p ", Payment.class )
.getResultList();
This query names the Payment entity explicitly. However, all subclasses of Payment are also available to the query. So, if the CreditCardPayment and WireTransferPayment entities extend the Payment class, all three types would be available to the entity query, and the query would return instances of all three.
This behavior can be altered in two ways:
by limiting the query to select only from the subclass entity
by using either the org.hibernate.annotations.Polymorphism annotation (global, and Hibernate-specific). See the @Polymorphism section for more info about this use case.
The HQL query from java.lang.Object is totally valid (although not very practical from a performance perspective)!
It returns every object of every entity type defined by your application mappings.
Comments
Post a Comment