I just noticed this bevahior at Hibernate and found this a bit awesome (in a welcoming way). I have one type of entity, called SyncItem , which serves as a superclass for a number of other objects. The inheritance strategy is set to InheritanceType.JOINED , so there is a separate table for all fields in the superclass, and subclass tables include only fields for everything added to the subclass, plus a link back to the superclass table.
So, for example, my SyncItem table looks like this:
mysql> describe syncItems; +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | hidden | bit(1) | NO | | NULL | | | title | varchar(255) | NO | | NULL | | | createDate | datetime | NO | | NULL | | | modifyDate | datetime | YES | | NULL | | | deleteDate | datetime | YES | | NULL | | +------------+--------------+------+-----+---------+----------------+
And if I have a subclass of Property , it can get a table like:
mysql> describe properties; +--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | syncId | bigint(20) | NO | PRI | NULL | | | propertyCode | varchar(255) | YES | | NULL | | +--------------+--------------+------+-----+---------+-------+
Now I have about a dozen different subclasses, and it surprised me that I have a query like:
@NamedQuery( name="SyncItem.findByTitle", query="SELECT s FROM SyncItem s WHERE s.title = :title")
When I wrote this, I expected that, as I select from SyncItem , I would only return the information contained in the superclass table (i.e. to get an instance of SyncItem , not a Property example). However, when I ran it, I found that the query actually returns the correct instance of the subclass. This is good because it simplifies the implementation of a number of things.
However, I wonder how JPA / Hibernate can return a subclass? He does not know that this title will be displayed on Property , and not on something else, and, as far as I can tell, it does not have a direct mapping from syncItems to properties (instead, it should display the opposite from properties to syncItems ). Is there a connection to each individual subclass table when this query is executed? If so, does that not make the request very expensive?
So yes, itβs just interesting whatβs going on behind the scenes.