We're updating the issue view to help you get more done. 

Changing @ManyToMany <-> @OneToMany doesn't add/remove unique constraint

Description

When having a @OneToMany annotation on an entity and later changing that to @ManyToMany, Hibernate does not drop the unique constraint. It simply leaves the constraint intact.

Dropping and letting Hibernate regenerate the table does not add the unique constraint (works as expected). This means you'll not be able to insert multiple entries in the table, unless you regenerate it.

Created Table thus far:

1 2 3 4 5 6 7 8 9 10 11 12 CREATE TABLE A_B ( A_id INT NOT NULL, b_id INT NOT NULL, CONSTRAINT PK__A_B__B54FFDE90F6D37F0 PRIMARY KEY (A_id, b_id) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = OFF, ALLOW_PAGE_LOCKS = OFF) ) GO CREATE UNIQUE INDEX UQ__A_B__4E29C30C1249A49B ON A_B (b_id ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = OFF, ALLOW_PAGE_LOCKS = OFF) GO )

Likewise, changing @ManyToMany to @OneToMany will add a unique constraint. Inserting multiple values will however work fine.

Re-generating the table will add the constraint just fine.

SQL thus far:

1 2 3 4 5 6 CREATE TABLE A_B ( A_id INT NOT NULL, b_id INT NOT NULL, CONSTRAINT PK__A_B__B54FFDE905E3CDB6 PRIMARY KEY (A_id, b_id) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ) GO

Attached are the pretty basic entities I've used to confirm this behaviour.

Environment

Hibernate 4.1.1, bundled with JBoss AS 7.1.1
Microsoft SQL Server 2008

Status

Assignee

Unassigned

Reporter

Marcus Bauer

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

4.1.1

Priority

Minor