Selecting MappedSuperclass from the database (Hibernate)

前端 未结 3 853
醉酒成梦
醉酒成梦 2021-01-01 23:40

Problem

I have a @MappedSuperclass called Data as the parent of every Entity in my database. It contains common attributes like Id etc. I then have an

相关标签:
3条回答
  • 2021-01-02 00:07

    I seem to be able to do this (although using InheritanceType.JOINED) with hibernate 5.0.8, java 1.8.0_73 and Oracle 12c - either I am misunderstanding or maybe hibernate has changed..

    I have the following hierarhcy:

    @MappedSuperclass
    @Inheritance(strategy=InheritanceType.JOINED)
    CommonRoot
     |
     |  @MappedSuperclass
     +- Mapped
            |  @Entity(name="Concrete1")
            |  @Table(name="CON1")
            +- Concrete1
            |
            |  @Entity(name="Concrete2")
            |  @Table(name="CON2")
            +- Concrete2
    

    And I can do the following HQL:

    SELECT entityId FROM com.hibernatetest.Mapped ORDER BY entityId ASC
    

    which gives these 2 SQL statements:

    select concrete2x0_.entityId as col_0_0_ from CON2 concrete2x0_ order by concrete2x0_.entityId ASC
    select concrete1x0_.entityId as col_0_0_ from CON1 concrete1x0_ order by concrete1x0_.entityId ASC
    

    and the warning

    WARN: HHH000180: FirstResult/maxResults specified on polymorphic query; applying in memory!
    

    Not sure what they mean though, as this can be done with SQL as:

    (select entityId from CON2
    union all
    select entityId from CON1)
    order by entityId ASC
    

    (And you can also add limit/rownum clauses to that if you desire, although that gets a bit clunky:

    select * from (
    (select * from (select entityId from CON2 order by entityId ASC) where rownum <= 10)
    UNION ALL
    (select * from (select entityId from CON1 order by entityId ASC) where rownum <= 10)
    ) where rownum <= 10 order by entityId ASC
    

    Not sure why hibernate shouldn't be able to do this - might suggest it to them.)

    0 讨论(0)
  • 2021-01-02 00:11

    No if you are using @MappedSuperclass

    The reason for this is that when you define base class as @MappedSuperclass, there is no table generated for the base class, instead all of the properties are replicated in the concrete tables. In your example only FullTimeEmployee, PartTimeEmployee and Store tables would exist.

    If you want to be able to query for base class entities you need to select different mapping for base classes. Use @Inheritance annotation on the base class and select one of the 3 possible mapping strategies - SINGLE TABLE, TABLE PER CLASS or JOINED

    0 讨论(0)
  • 2021-01-02 00:14

    Yes

    FROM Employee WHERE Employee.<employee only properties> = someValue
    

    But only, as the others have said here, if the Employee entity is mapped. You don't need to even map it to its' own table. See the mapping strategies in Hibernate.

    0 讨论(0)
提交回复
热议问题