-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Customize unique constraint name for many-to-one
PostPosted: Fri Oct 07, 2016 8:32 am 
Beginner
Beginner

Joined: Tue Dec 26, 2006 3:50 pm
Posts: 22
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


Top
 Profile  
 
 Post subject: Re: Customize unique constraint name for many-to-one
PostPosted: Fri Oct 07, 2016 11:43 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
It looks like a bug. Please provide a replicating test case and open a Jira issue.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Customize unique constraint name for many-to-one
PostPosted: Mon Oct 10, 2016 7:12 am 
Beginner
Beginner

Joined: Tue Dec 26, 2006 3:50 pm
Posts: 22
Please see https://hibernate.atlassian.net/browse/HHH-11165


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.