Hibernate ORM
  1. Hibernate ORM
  2. HHH-7797

Inconsistent logic by uses of Dialect#supportsNotNullUnique

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 4.1.8, 4.3.0.Beta1
    • Fix Version/s: 4.2.0.CR1, 4.1.10, 4.3.0.Beta1
    • Component/s: None
    • Labels:
      None
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true

      Description

      Dialect#supportsNotNullUnique is used by 2 areas:

      1.) Table. Prevents "not null unique" if supportsNotNullUnique is false and the column is not nullable.
      2.) UniqueKey. The logic is reversed from #1. Appears to prevent unique constraints from being created if supportsNotNullUnique is false and one of the constraint's columns is nullable.

      The uses seem inconsistent and incorrect.

      This came about through HHH-5376, HHH-7579, and HHH-7578. Some dialects appear to differ in their support of "unique not null" and unique constraints on nullable fields. The sole use of supportsNotNullUnique probably won't cut it.

      Test case: o.h.test.collection.map.hhh7557.EntityMapTest. All entities have primary keys and explicitly define them as unique and not nullable. The test fails on Oracle as it attempts to use "not null unique" on the column, as well as create a primary key constraint.

        Issue Links

          Activity

          Hide
          Brett Meyer added a comment -

          Silvano Maffeis, there used to be a handful of them, but the logic got messy and confusing. That's precisely why we removed them, then created the UniqueDelegate.

          Show
          Brett Meyer added a comment - Silvano Maffeis , there used to be a handful of them, but the logic got messy and confusing. That's precisely why we removed them, then created the UniqueDelegate.
          Hide
          Silvano Maffeis added a comment -

          I've created a Dialect and a UniqueDelegate subcIass and I was able to suppress the ALTER TABLE statements by that.
          However I couldn't get the unique(...) statement to be appended to the "CREATE TABLE ..." statements. So I looked at the 4.2.2 sources and
          found a disabled line of code in UniqueKey.java. I think this is the reason why I can't manage to get the unique constraints on the CREATE TABLE.
          Is my suspicion wrong?

          @Override
          public String sqlConstraintString(
          Dialect dialect,
          String constraintName,
          String defaultCatalog,
          String defaultSchema)

          { // return dialect.getUniqueDelegate().uniqueConstraintSql( this ); // Not used. return ""; }
          Show
          Silvano Maffeis added a comment - I've created a Dialect and a UniqueDelegate subcIass and I was able to suppress the ALTER TABLE statements by that. However I couldn't get the unique(...) statement to be appended to the "CREATE TABLE ..." statements. So I looked at the 4.2.2 sources and found a disabled line of code in UniqueKey.java. I think this is the reason why I can't manage to get the unique constraints on the CREATE TABLE. Is my suspicion wrong? @Override public String sqlConstraintString( Dialect dialect, String constraintName, String defaultCatalog, String defaultSchema) { // return dialect.getUniqueDelegate().uniqueConstraintSql( this ); // Not used. return ""; }
          Hide
          Brett Meyer added a comment -

          Silvano, no that's a different case (this area is confusing).

          Make sure you're implementing either getColumnDefinitionUniquenessFragment(org.hibernate.mapping.Column column) or getTableCreationUniqueConstraintsFragment(org.hibernate.mapping.Table table). The first should return " unique", if you want the column definition itself to have the unique syntax. The second would loop through the Table#getUniqueKeyIterator and return something like " unique(column1, column2)", if you want the unique column definitions to be added to the end of the create table statement.

          This is all described in the UniqueDelegate javadoc

          Show
          Brett Meyer added a comment - Silvano, no that's a different case (this area is confusing). Make sure you're implementing either getColumnDefinitionUniquenessFragment(org.hibernate.mapping.Column column) or getTableCreationUniqueConstraintsFragment(org.hibernate.mapping.Table table). The first should return " unique", if you want the column definition itself to have the unique syntax. The second would loop through the Table#getUniqueKeyIterator and return something like " unique(column1, column2)", if you want the unique column definitions to be added to the end of the create table statement. This is all described in the UniqueDelegate javadoc
          Hide
          Silvano Maffeis added a comment -

          Hi Brett. Those APIs are part of 4.3.0.Beta3, but I can't find them in the 4.2.2.Final. I was looking for a solution in 4.2.2.Final.

          Show
          Silvano Maffeis added a comment - Hi Brett. Those APIs are part of 4.3.0.Beta3, but I can't find them in the 4.2.2.Final. I was looking for a solution in 4.2.2.Final.
          Hide
          Brett Meyer added a comment -

          Silvano Maffeis, UniqueDelegate and its hook into Dialect have been there since 4.1.x and 4.2.0.

          https://github.com/hibernate/hibernate-orm/tree/4.2/hibernate-core/src/main/java/org/hibernate/dialect/unique

          Show
          Brett Meyer added a comment - Silvano Maffeis , UniqueDelegate and its hook into Dialect have been there since 4.1.x and 4.2.0. https://github.com/hibernate/hibernate-orm/tree/4.2/hibernate-core/src/main/java/org/hibernate/dialect/unique

            People

            • Votes:
              4 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development