How do I join tables on non-primary key columns in secondary tables?

后端 未结 3 1625
攒了一身酷
攒了一身酷 2021-01-11 15:09

I have a situation where I need to join tables on an object in an ORM class hierarchy where the join column is NOT the primary key of the base class. Here is an example of

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-11 15:22

    You will not be able to do what you want. @CollectionOfElements (and @OneToMany, for that matter) are always mapped via owner's entity primary key.

    The way you map Foo / Bar inheritance is rather strange as well - they clearly are not in the same table; seems like using JoinedSubclass would be a better approach. Keep in mind that still won't help you map bar_names to bar_id because primary key value is shared among the hierarchy (even though column name may be different for subclass).

    A possible alternative is to use @OneToOne mapping between Foo and Bar instead of inheritance. That's the only way you will be able to map bar_names to bar_id and the most appropriate mapping for you table structure (though, perhaps, not for your domain model).

提交回复
热议问题