Hibernate Inheritance Strategy and Why

后端 未结 3 1855
感情败类
感情败类 2020-12-18 13:53

I have 3 non-abstract persist-able classes. MyClubUser and HisClubUser classes inherit from User class. I use one table per subclass strategy i.e. @Inheritance(strateg

3条回答
  •  执笔经年
    2020-12-18 14:49

    Why Hibernate does that [joining other tables] where my concern is only User?

    Because all HisClubUser are also valid instances of User, it is logical to retrieve these when you ask for User.

    My point is even though the data is retrieved, I would not able to access these properties in MyClubUser or HisClubUser given that User instances are returned.

    Are you really sure? Check (in debug for example) the effective class that is returned, it should be the subclasses. So down-casting could be possible, and the properties could be accessed.

    Also, does this causes additional overhead as compared to a query where it just query User table without the left outer join?

    Yes, and extra join has an overhead. It could be important or not : I suggest you test it in your specific case.

提交回复
热议问题