Superclass @UniqueConstraint annotation wrongly applied to subclass in TABLE_PER_CLASS hierarchy

Description

Superclass @UniqueConstraint annotation are wrongly applied to subclass in TABLE_PER_CLASS hierarchy.

Subclass table should not be inheriting superclass annotations, using the same unique key name in subclass does not seem to override the annotation and the superclass annotation is still applied under a different name.

Hibernate generated code:

DB tables:

Environment

Windows 10 Home, Intel i7, Java 1.8.0_261, JavaEE 7, Hibernate 5.4.21, MariaDB 10.1.41

Activity

Show:
Steve Ebersole
October 15, 2020, 12:12 PM
Edited

And since you are quoting the spec, could you please show me where in that section you think it says that reusing constraint names specifically is supported? And to pre-empt… the spec not saying that something is unsupported != it is supported. What is supported by the spec is what the spec explicitly says is supported. So where is that sentence(s)?

Vlad Paln
October 15, 2020, 1:29 PM
Edited

JSR 338: JPA: 2.12.3 specifically mentions column inheritance from superclass, no other table parameter inheritance is mentioned in the specs.

OFF TOPIC: specs don't mention anything about unique constraints in regards to this issue. As mentioned above this was never an issue, but an attempt to circumvent the bug in Hibernate by overriding the unique constraint name in superclass. You are correct that some databases like MS SQL require unique constraint names per schema, whereas dbs like MySQL and MariaDB only require unique names per table, so this rule should definitely stay in place. Correct me if I'm mistaken but currently if Hibernate comes across the same name on a db that disallows this it will automatically assign a generated name.

That said same unique constraint names where never part of the issue the inheritance of said constraints from superclass was and where it violated the specs.

I think the way I phrased some of my comments has confused the issue some in regards to specs and unique constraint names, sorry about that.

Steve Ebersole
October 15, 2020, 1:40 PM

As a general rule… if a user gives us explicit configuration (here you do, you explicitly configure the constraint name) we never disregard that. Seems completely reasonable if you step back and think about it. So no, you are mistaken about that part. Now, if you leave off the names, yes, Hibernate will automatically generate unique names.

That said same unique constraint names where never part of the issue the inheritance of said constraints from superclass was and where it violated the specs.

Never said it was. I simply pointed out that even when the bug is fixed your model still would not work because of the duplicated constraint names

Vlad Paln
October 15, 2020, 2:26 PM
Edited

Than might be the case.

Nathan Xu
October 15, 2020, 4:35 PM

As a general rule, unique constraint name should be unique within db. MySQL and Mariadb happens to be exception. See https://stackoverflow.com/questions/1397671/can-there-be-constraints-with-the-same-name-in-a-db

In my created PR’s testing case, I simplified the scenario to get rid of child table constraint to avoid confusion. I think as long as we achieve the goal to disallow parent table constraint to be created for child table, we are done.

Fixed

Assignee

Nathan Xu

Reporter

Vlad Paln

Fix versions

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure