Someone has been doing the following so that multiple associations with entities within a joined inheritance hierarchy share the same foreign key: 1) each association that shares the same foreign key uses @OneToMany( targetEntity=<superclass> ) so that the FK is in the superclass; 2) each association uses a filter to only include entities with a particular discriminator value.
The SQL for loading each of these associations involves an outer join to each subclass table, then the filter restricts to the particular entity class of interest for the particular association. I'm trying to figure out if there is a JPA-supported way that would not involving outer joining all the subclass tables (e.g., using the parameterized type of the collection as default for targetEntity along with @JoinColumn(table="superclass_table") ). Thanks, Gail On Tue, Feb 16, 2016 at 10:48 AM, Emmanuel Bernard <emman...@hibernate.org> wrote: > There is a case about association shared in a inheritance case that I > don't fully recollect. I was against supporting as it did break OO when > you thought about it - at least when I thought about it. > But this case seems to be different. Does someone explicitly asks for that > use case ? > > > On 13 févr. 2016, at 22:56, Gail Badner <gbad...@redhat.com> wrote: > > > > For joined inheritance, such as: > > > > @Entity > > @Inheritance(strategy = InheritanceType.JOINED) > > @Table(name = "task_base") > > public class TaskBase { ... } > > > > @Entity > > @Table(name = "task") > > public class Task extends TaskBase { ... } > > > > Does JPA allow mapping a one-to-many association with the foreign key > > column in a superclass table (task_base)? For example: > > > > @Entity > > public class Goal { > > ... > > @OneToMany(targetEntity = TaskBase.class) > > @JoinColumn(name = "goal_id", table = "task_base") > > private Set<Task> tasks = new HashSet<Task>(); > > ... > > } > > > > Currently, Hibernate throws: > org.hibernate.cfg.NotYetImplementedException: > > Collections having FK in secondary table. > > > > As you can see. the foreign key is actually in the superclass table. > > > > JPA 2.1 spec says this for the description of @JoinColumn( name="..." ) > > when used for a unidirectional one-to-many association: > > > > "If the join is for a unidirectional OneToMany mapping using a foreign > key > > mapping strategy, the foreign key is in the table of the target entity." > > > > Is "the table of the target entity" just a default that can be overridden > > by the "table" attribute? If so, then this is a bug in Hibernate. > > > > Another question, does JPA allow multiple associations to use the same > > foreign key column? For example: > > > > @Entity > > @Inheritance(strategy = InheritanceType.JOINED) > > @Table(name = "task_base") > > public class TaskBase { ... } > > > > @Entity > > @Table(name = "task") > > public class Task extends TaskBase { ... } > > > > @Entity > > @Table(name = "othertask") > > public class OtherTask extends TaskBase { ... } > > > > @Entity > > public class Goal { > > ... > > @OneToMany > > @JoinColumn(name = "goal_id", table = "task_base") > > private Set<Task> tasks = new HashSet<Task>(); > > > > @OneToMany > > @JoinColumn(name = "goal_id", table = "task_base") > > private Set<OtherTask> otherTasks = new HashSet<OtherTask>(); > > ... > > } > > > > The above also fails with NotYetImplementedException for the same reason. > > > > I've created a pull request with this test case. [1] > > > > When I switched to use single table inheritance, there was no failure, > but > > when Goal.tasks is loaded, it contained both Task and OtherTask objects. > > > > Is this an invalid use case or a Hibernate bug? > > > > Thanks, > > Gail > > > > [1] https://github.com/hibernate/hibernate-orm/pull/1265 > > _______________________________________________ > > hibernate-dev mailing list > > hibernate-dev@lists.jboss.org > > https://lists.jboss.org/mailman/listinfo/hibernate-dev > _______________________________________________ hibernate-dev mailing list hibernate-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/hibernate-dev