Here is an example HBM mapping:
Code:
<hibernate-mapping package="org.hibernate.test" default-access="field">
<class entity-name="TestWorkflowItem" name="WorkflowItem" table="test_workflowitem">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="name" column="name" not-null="true" type="string"/>
<many-to-one cascade="all" entity-name="TestWorkflowItem" column="next_id" unique="true" name="next" foreign-key="fk_testworkflowitem_workflowitem"/>
</class>
</hibernate-mapping>
The many-to-one association specifies unique="true".
Consequently, Hibernate 5.0.11 generates a unique constraint for this:
Code:
alter table test_workflowitem add constraint UK_kewmkhh9wckso1vdmvph1l98p unique (next_id)
Now, I want to customize this constraint name. I can do that by using unique-key="uk_nextid" instead of unique="true" in the many-to-one mapping:
Code:
alter table test_workflowitem add constraint uk_nextid unique (next_id)
This does not work anymore, when the many-to-one mapping has a delete-orphan cascading:
Code:
<many-to-one cascade="all-delete-orphan" entity-name="TestWorkflowItem" column="next_id" unique-key="uk_nextid" name="next" foreign-key="fk_testworkflowitem_workflowitem"/>
It triggers the following mapping exception:
Code:
Caused by: org.hibernate.boot.MappingException: many-to-one attribute [TestWorkflowItem.next] specified delete-orphan but is not specified as unique; remove delete-orphan cascading or specify unique="true" : origin(org/hibernate/test/TestWorkflowItem.hbm.xml)
at org.hibernate.boot.model.source.internal.hbm.ModelBinder.createManyToOneAttribute(ModelBinder.java:2174)
at org.hibernate.boot.model.source.internal.hbm.ModelBinder.bindAllEntityAttributes(ModelBinder.java:1215)
at org.hibernate.boot.model.source.internal.hbm.ModelBinder.bindRootEntity(ModelBinder.java:264)
at org.hibernate.boot.model.source.internal.hbm.ModelBinder.bindEntityHierarchy(ModelBinder.java:184)
at org.hibernate.boot.model.source.internal.hbm.HbmMetadataSourceProcessorImpl.processEntityHierarchies(HbmMetadataSourceProcessorImpl.java:144)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:218)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.testing.junit4.BaseCoreFunctionalTestCase.buildSessionFactory(BaseCoreFunctionalTestCase.java:107)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.hibernate.testing.junit4.TestClassMetadata.performCallbackInvocation(TestClassMetadata.java:200)
... 12 more
When using the mapping
Code:
<many-to-one cascade="all-delete-orphan" entity-name="TestWorkflowItem" column="next_id" unique="true" unique-key="uk_nextid" name="next" foreign-key="fk_testworkflowitem_workflowitem"/>
the mapping exception goes away, but the specified unique-key name "uk_nextid" is ignored.
Is the latter a bug or how to specify a unique key name for a many-to-one assocation with delete-orphan cascading?
Thanks,
Holger