[hibernate-dev] HHH-10500 and multiple associations using the same foreign key column

Emmanuel Bernard emmanuel at hibernate.org
Tue Feb 16 13:48:30 EST 2016


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 <gbadner at 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 at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/hibernate-dev



More information about the hibernate-dev mailing list