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
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).