Selecting MappedSuperclass from the database (Hibernate)

前端 未结 3 870
醉酒成梦
醉酒成梦 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条回答
  •  萌比男神i
    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.)

提交回复
热议问题