Sometimes hibernate doesn't create constraint for foreign key in ManyToOne relationship

Description

I have next hierarchy of classes/entities

In code above I've skipped all the setters, getters and constructors.

When I run application, hibernate creates tables with all required columns but doesn't create constraint for foreign key for next field in table UserTask.

Nevertheless constraint for the field "private Task task;" is being created successfully.
In the execution output I see

I use update hbm2ddl.auto property

Environment

Windows 10, MS SQL 2016

Activity

Show:
Christian Beikov
March 4, 2017, 1:38 AM

I added a fix now that should handle this special case where "only a single table" in the TABLE_PER_CLASS strategy exists.

Ivan Fedorov
March 4, 2017, 11:23 AM
Edited

Hi Christian,
Thanks for your reply.
>>To what table should it create a FK to?
I supposed that it should create FK to table RegularUser.
>>You entity User uses the TABLE_PER_CLASS strategy, so every subclass will have a different table
Right. And as I understand every subclass has its own private Long userId property inherited from base class and as result every table will have its own column UserID so why hibernate can't just use this property to create FK to it.
>>Do you have any other subclasses of User?
Yes, I do. I have several subclasses of abstract class User and more over, some of that subclasses also have subclasses.
To be more precise I have hierarchy like next

and so on.
The strange thing here for me is that I have two other classes with @JoinColumn annotation similar to hierarchy above and in that case all works fine.
These are those classes

Christian Beikov
March 4, 2017, 11:33 AM
Edited

Well in the code I tested it worked fine. I guess it makes a difference if you have further subtypes i.e. if Company had other subtypes, then the FK would not be generated. This is the case with your RegularUser apparently since it has subtypes Admins and Editors. Since instances of these subtypes do not have an entry in the table for RegularUser, Hibernate can't add a FK constraint from UserTask to RegularUser. That would be wrong. What you maybe want, is to have SINGLE_TABLE inheritance for subclasses of RegularUser, then there wouldn't be the tables Admins and Editors but a discriminator column would be introduced in the RegularUsers table to distinguish the subtypes. Unfortunately this is not yet possible. For further information see

Ivan Fedorov
March 4, 2017, 12:13 PM

Yes, now I see the reason. Thank you!

Andrea Boriero
March 8, 2017, 1:09 PM

I agree with .
I'm going to reject this jira.

Assignee

Christian Beikov

Reporter

Ivan Fedorov

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure