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

Gail Badner gbadner at redhat.com
Sat Feb 13 16:56:15 EST 2016


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


More information about the hibernate-dev mailing list