| Some background about this issue can be found in the Hibernate forum here: https://forum.hibernate.org/viewtopic.php?f=1&t=1043689&sid=333833f9c9690a36a4b468f5c32390c6 The issue is that it's not possible with recent Hibernate 5.x versions to customize the unique key name on a many-to-one association with the unique-key attribute that has also a orphan delete cascading. (The upcoming attachment has a complete test case) The following mapping ...
<many-to-one cascade="all-delete-orphan" entity-name="TestWorkflowItem" column="next_id" unique-key="uk_nextid" name="next" foreign-key="fk_testworkflowitem_workflowitem"/>
...causes a mapping Exception:
org.hibernate.testing.junit4.CallbackException: org.hibernate.testing.junit4.BaseCoreFunctionalTestCase#buildSessionFactory
at org.hibernate.testing.junit4.TestClassMetadata.performCallbackInvocation(TestClassMetadata.java:203)
at org.hibernate.testing.junit4.TestClassMetadata.invokeCallback(TestClassMetadata.java:187)
at org.hibernate.testing.junit4.TestClassMetadata.performCallbacks(TestClassMetadata.java:181)
at org.hibernate.testing.junit4.TestClassMetadata.performBeforeClassCallbacks(TestClassMetadata.java:172)
at org.hibernate.testing.junit4.BeforeClassCallbackHandler.evaluate(BeforeClassCallbackHandler.java:25)
at org.hibernate.testing.junit4.AfterClassCallbackHandler.evaluate(AfterClassCallbackHandler.java:25)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
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.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
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)
... 18 more
If the many-to-one mapping is changed to also include unique="true", the mapping exception goes away, but the specified unique key name "uk_nextid" is ignored and not used. Instead an auto-generated key name is used:
alter table test_workflowitem add constraint UK_kewmkhh9wckso1vdmvph1l98p unique (next_id)
|