OpenJPA
  1. OpenJPA
  2. OPENJPA-756

Mapping tool fails with duplicate unique constraint on @Id field if @Column(unique=true)

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.0.0-M2
    • Fix Version/s: None
    • Component/s: sql
    • Labels:
      None
    • Environment:
      Derby, Oracle, possibly other DBs

      Description

      The runtime mapping tool (openjpa.jdbc.SynchronizeMappings=buildSchema) fails to create a table if the @Column annotation with unique=true attribute is specified on a field tagged with @Id. ex:

      @Id
      @Column(name="uniqueid", unique=true)
      private int id;

      OpenJPA generates a unique constraint on the field along with the primary key constraint. Derby, Oracle, and possibly other databases fail with a message to the effect that OpenJPA is trying to create a duplicate constraint. Primary keys are unique by definition so OpenJPA should not create the unnecessary extra unique constraint.

      Here's an example of the failure using Derby:

      Constraints 'UNQ_' and 'SQL081030035840490' have the same set of columns, which is not allowed.

      {stmnt 2175170 CREATE TABLE UniqueIDEntity (uniqueid INTEGER NOT NULL, name VARCHAR(255), PRIMARY KEY (uniqueid), CONSTRAINT UNQ_ UNIQUE (uniqueid))}

      [code=30000, state=42Z93]

      Some database may allow/require this behavior, so investigation will need to be done to determine if there will be special handling for some databases vs. others.

      The simple workaround is to remove the unique attribute on an @Id column.

        Activity

        Hide
        Denis Nedelyaev added a comment -

        1.2.1 is also affected.

        Show
        Denis Nedelyaev added a comment - 1.2.1 is also affected.
        Hide
        A. L. added a comment -

        It seems, all UNIQUE constraints are created with the name UNQ_!

        Show
        A. L. added a comment - It seems, all UNIQUE constraints are created with the name UNQ_!
        Hide
        Jeremias Maerki added a comment -

        If the primary key field is a String (ex. a URI as in my case), even the work-around doesn't help:

        @Id
        @Column(name="URI")
        public String getUriString()

        { return this.uriString; }

        OpenJPA 1.2.1 creates for Derby:
        CREATE TABLE RepresentationType (URI VARCHAR(255) NOT NULL, contentType VARCHAR(255), PRIMARY KEY (URI), CONSTRAINT UNQ_URI UNIQUE (URI))

        Show
        Jeremias Maerki added a comment - If the primary key field is a String (ex. a URI as in my case), even the work-around doesn't help: @Id @Column(name="URI") public String getUriString() { return this.uriString; } OpenJPA 1.2.1 creates for Derby: CREATE TABLE RepresentationType (URI VARCHAR(255) NOT NULL, contentType VARCHAR(255), PRIMARY KEY (URI), CONSTRAINT UNQ_URI UNIQUE (URI))

          People

          • Assignee:
            Unassigned
            Reporter:
            Jeremy Bauer
          • Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:

              Development