kundera icon indicating copy to clipboard operation
kundera copied to clipboard

Polymorphic queries support - selecting base class entity and expecting all subclasse entities to be returned

Open AntonYudin opened this issue 8 years ago • 2 comments

Looks like Kundera does not support polymorphic queries. When trying to SELECT * FROM BaseEntity, the query returns only BaseEntity and ignores any existing subclassed entities.

@Entity
@Table(name = "events")
@Inheritance(strategy = javax.persistence.InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "typed")
public class Event implements java.io.Serializable {
        @EmbeddedId
        private EventId id;
...

@Entity
@Table(name = "itemadded")
@DiscriminatorValue("ItemAdded")
public class ItemAdded extends Event {
...

final javax.persistence.Query query = entityManager.createQuery(
        "SELECT e FROM Event e"
);

All returned instances are of the type "Event", even though there are entities of type "ItemAdded". Persisting works as expected - the logged queries show insert statements with appropriate columns.

Doing find(ItemAdded.class, id) works as expected.

In other JPA implementations (hibernate for example), one would expect all Subclassed entities to be returned even when only bass class is passed to the createQuery or Criteria API's select() method.

Is there a workaround or a solution planned?

Thanks.

AntonYudin avatar Jul 04 '17 18:07 AntonYudin

Hi @AntonYudin,

Please share your sample project so that I can replicate your issue at my end.

-Dev

devender-yadav avatar Jul 06 '17 10:07 devender-yadav

Here is the spec talking about polymorphic queries support.

4.4.8 Polymorphism Java Persistence queries are automatically polymorphic. The FROM clause of a query designates not only instances of the specific entity class(es) to which it explicitly refers but instances of subclasses of those classes as well. The instances returned by a query thus include instances of the subclasses that satisfy the query criteria. Non-polymorphic queries or queries whose polymorphism is restricted can be specified using entity type expressions in the WHERE clause to restrict the domain of the query. See section 4.6.17.5.

AntonYudin avatar Jul 10 '17 14:07 AntonYudin