Search code examples
hibernatejpahqlnamed-query

Polymorphic query with JPA and mapped superclass to return count of a particular concrete entity


I have a BaseEntity abstract class which is a super class for all my entities. This is a MappedSuperClass. Now I want to define a named query preferably on the base-entity such that it returns me the count of a specific sub class entity. So for e.g if Class A subclasses BaseEntity I can run the named query for class A and return the count of A i.e. the number of rows of Class A present in the database. Is it possible with JPA 1.0 and hibernate as the implementer? (I want to avoid putting a named query on each of my entities other than the base entity to return the count) Thanks, -v-


Solution

  • you can query for it

    int count = session.createCriteria(Subclass.class)
        .setProjection(Projections.rowCount())
        .uniqueResult();