Please consider the following HBM mapping and Java class:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="org.hibernate.test" default-access="field">
<class entity-name="TestWorkflowItem" name="WorkflowItem2" table="test_workflowitem">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="name" column="name" not-null="true" type="string"/>
<list name="childs" table="test_workflowitem_childs" cascade="all" access="field" lazy="false">
<key column="item" foreign-key="fk_workflowitem"/>
<index column="display_order"/>
<many-to-many column="child" entity-name="TestWorkflowItem" unique="true" foreign-key="fk_workflowitem_childs" lazy="false"/>
</list>
</class>
</hibernate-mapping>
Code:
package org.hibernate.test;
import java.util.List;
public final class WorkflowItem2 {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private List<WorkflowItem2> childs;
public WorkflowItem2() {
}
public void setChilds(List<WorkflowItem2> newChilds) {
childs = newChilds;
}
public List<WorkflowItem2> getChilds() {
return childs;
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
}
The WorkflowItem contains a list mapping with a unique many-to-many element.
Hibernate 4.3.11 generates the following table statements:
Code:
create table test_workflowitem (
id bigint not null,
name varchar(255) not null,
primary key (id)
)
create table test_workflowitem_childs (
item bigint not null,
child bigint not null,
display_order integer not null,
primary key (item, display_order)
)
alter table test_workflowitem_childs
add constraint UK_ojsoe2oi4ydfh13jnq8cv1k3 unique (child)
alter table test_workflowitem_childs
add constraint fk_workflowitem_childs
foreign key (child)
references test_workflowitem
alter table test_workflowitem_childs
add constraint fk_workflowitem
foreign key (item)
references test_workflowitem
Hibernate 5.0.10 and 5.2.2 generate the following:
Code:
create table test_workflowitem (
id bigint not null,
name varchar(255) not null,
primary key (id)
)
create table test_workflowitem_childs (
item bigint not null,
display_order integer not null,
child bigint not null,
primary key (item, display_order)
)
alter table test_workflowitem_childs
add constraint fk_workflowitem_childs
foreign key (child)
references test_workflowitem
alter table test_workflowitem_childs
add constraint fk_workflowitem
foreign key (item)
references test_workflowitem
As you see, the unique constraint for the column 'child' is not generated anymore with Hibernate 5.
Is this intentional or a bug?
Thanks,
Holger