8

I have created the following entity bean, and specified two columns as being unique. Now my problem is that the table is created without the unique constraint, and no errors in the log. Does anyone have an idea?

@Entity
@Table(name = "cm_blockList", uniqueConstraints = @UniqueConstraint(columnNames = {"terminal", "blockType"}))
public class BlockList {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="terminal")
    private Terminal terminal;
    @Enumerated(EnumType.STRING)
    private BlockType blockType;
    private String regEx;
}
homaxto
  • 5,428
  • 8
  • 37
  • 53

6 Answers6

7

Well, I found another way to make the design. More because the design evolved than a work around.
I heard however from a colleague, that had had the same problem, that unique constraint only are created by hibernate (we are running JBoss 4.3) when the entire database is created. It will not work when you create a new table in an existing database.
So in persistence.xml it is necessary to set hibernate.hbm2ddl.auto to create-drop to make it work. I can not confirm this though.

homaxto
  • 5,428
  • 8
  • 37
  • 53
  • homaxto your observation is correct. I faced the same situation and the constraint works when create-drop is set. –  May 15 '11 at 03:09
  • I have had same problem, and re-creating the whole database, worked – greuze Oct 03 '11 at 08:17
  • 1
    I tested it by re-creating the whole database by using all the 3 options: `update`, `create` and `create-drop` and it didn't work for me for any of the three options. Logically also, it doesn't make sense to make it work with just `create-drop` and not with `update` and `create`. Seems like a bug to me in Hibernate. But then again I wonder how come this bug is not yet solved by 2021 (10 years old bug ??). – Amit Jan 08 '21 at 21:00
  • I got the same issue. This solved my issue https://stackoverflow.com/a/76016256/9370144 – Nadun Kulatunge Apr 14 '23 at 14:43
3

You dont have to drop everything, just drop this particular table you want to create unique index for

Dima
  • 1,045
  • 14
  • 23
0

If you want the unique constraint, you should do it by sql:

ALTER TABLE cm_blockList ADD CONSTRAINT cm_blockList_unq UNIQUE (terminal);

See also: https://stackoverflow.com/a/3498242/1244488

Community
  • 1
  • 1
huangyp
  • 119
  • 6
0

I also faced the same issue, but adding unique-key="UK1_test" in the mapping file worked fine.

<property name="hibernate.hbm2ddl.auto">create</property> with unique-key="UK1_test" unique="true" in the mapping file created alter table employee add constraint UK1_test unique (addrId)

Ritesh Kaushik
  • 715
  • 2
  • 13
  • 24
0

More suggestions than a real answer...

  • you should clarify which JPA provider (and the version) you are using for such questions.

  • I notice that one of your column is part of a ManyToOne association, this might be a "special" case not well handled by your JPA provider. By not well handled, I mean bug. I'd check the issue tracker for an existing issue (and maybe create a new one, your case seems JPA compliant).

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
0

These days I meet the same problem when I add @Column(nullable = false, unique = true, length = 16) And not got a unique index.

After a few try with it, I found spring data jpa update database table(ddl) when fields change but not with the @Column changes. So assign unique = true after tables created is useless.To get a table with unique index,You need to drop the old one.Then re-run the code, let jpa create table again.

I believe it it because jpa doesn't know whether it contains same field value in old table when avoiding new unique index.So it accept a brand new table.

Tender
  • 11
  • 2