【问题标题】:Table per subclass (without using a discriminator)每个子类的表(不使用鉴别​​器)
【发布时间】:2015-10-21 14:53:07
【问题描述】:

10.1.2 节中。每个子类的表它讨论了通过跨多个表的一对一映射来创建继承。

https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#inheritance-tablepersubclass

当互联网上的所有示例都在谈论“每个子类的表”时,他们实际上是在谈论 10.1.3。每个子类的表:使用鉴别器

我的问题是 10.1.2.每个子类的表知道在没有鉴别器列的情况下要实例化哪个类。

如果答案是 hibernate 执行 3 次(或其他)额外查询来查找数据的位置,那么当鉴别器方法只保证 2 次查询时,为什么要使用此方法。

【问题讨论】:

    标签: hibernate discriminator


    【解决方案1】:

    如果您查看查询,它将有某种 switch 语句,如下所示

    select
        account0_.id as id1_9_,
        account0_.balance as balance2_9_,
        account0_1_.checkLimitAmount as checkLim1_10_,
        account0_2_.atmLimit as atmLimit1_11_,
        case 
            when account0_1_.id is not null then 1 
            when account0_2_.id is not null then 2 
            when account0_.id is not null then 0 
        end as clazz_ 
    from
        INHERITANCE_JTND_ACCOUNT account0_ 
    left outer join
        INHERITANCE_JTND_CHECKING_ACCOUNT account0_1_ 
            on account0_.id=account0_1_.id 
    left outer join
        INHERITANCE_JTND_SAVINGS_ACCOUNT account0_2_ 
            on account0_.id=account0_2_.id
    

    所以它只做一个查询。 Hibernate 然后使用clazz_ 列来确定要实例化的内容。上面的查询来自 HSQLDB,对于其他数据库引擎可能会有所不同。

    打印 JPA/Hibernate 生成的 SQL 语句(至少在您的本地环境中)通常是个好主意,因为有时您会对它生成的 DML 感到惊讶。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多