Efficiency of Hibernate's table-per-subclass inheritance strategy

前端 未结 3 530
孤独总比滥情好
孤独总比滥情好 2021-02-07 14:38

I\'m thinking about table layout for a Hibernate-managed class hierarchy, and certainly the table per subclass technique strikes me as the most appropriate in a general sense.

3条回答
  •  温柔的废话
    2021-02-07 15:11

    You'll find that Hibernate writes the query for an unknown animal type with a series of LEFT JOIN statements, one per subclass. So the query will slow as the number of subclasses increases, and will attempt to return an ever wider result set. So you are correct, it doesn't scale well with large class hierarchies.

    With HQL, yes you can query the subclass directly, and access its properties. That will then be rendered with a single INNER JOIN.

    I haven't tried this with multiple levels of inheritance. If the above hasn't put you off yet, suggest you try it and see - you can turn on SQL debug output to see what is being sent to the database, or simply profile your database.

提交回复
热议问题