Table per subclass inheritance relationship: How to query against the Parent class without loading any subclass ??? (Hibernate)

后端 未结 3 885
感动是毒
感动是毒 2020-12-05 08:57

Suppose a Table per subclass inheritance relationship which can be described bellow (From wikibooks.org - see here)

Notice Parent class is n

3条回答
  •  Happy的楠姐
    2020-12-05 09:36

    Update: It appears the first option doesn't work as I thought.

    First option:

    Specify the class in the where clause:

    select p from Project p where p.class = Project 
    

    Second option:

    Use explicit polymorphism that you can set using Hibernate's @Entity annotation:

    @javax.persistence.Entity
    @org.hibernate.annotations.Entity(polymorphism = PolymorphismType.EXPLICIT)
    @Inheritance(strategy = InheritanceType.JOINED)
    public class Project {
    
        @Id
        private long id;
        ...
    }
    

    This is what Hibernate Core documentation writes about explicit polymorphism:

    Implicit polymorphism means that instances of the class will be returned by a query that names any superclass or implemented interface or class, and that instances of any subclass of the class will be returned by a query that names the class itself. Explicit polymorphism means that class instances will be returned only by queries that explicitly name that class.

    See also

    • How to get only super class in table-per-subclass strategy?

提交回复
热议问题