-->
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.  [ 6 posts ] 
Author Message
 Post subject: Single-column unique constraint names in Hibernate 3 and 5
PostPosted: Tue Sep 13, 2016 3:37 am 
Beginner
Beginner

Joined: Tue Dec 26, 2006 3:50 pm
Posts: 22
While migrating from Hibernate 3 to 5 I noticed a difference in DDL generation for columns that specify "unique=true" in native HBM-Mappings.

While Hibernate 3 uses "unique" directly in the generated CREATE TABLE statement, Hibernate 5 creates an additional ALTER TABLE statement for creating the unique constraint.

So, in the former case, the target database will create the unique constraint name, while in the latter case, Hibernate will generate the unique constraint name, something like "UK_ej7ku5lkib9euyy8cl4je00b6".

I think one way to tackle that is to use unique-key="myname" instead of using unique="true" in the HBM mapping files, but that's some effort to change all mappings.
Therefore, another idea was to customize the ImplicitNamingStrategy implementation by overriding the method "determineUniqueKeyName". But unfortunately, this method doesn't seem to be called at all. Some debugging revealed that the member "uniqueConstraintHoldersByTable" in InFlightMetadataCollectorImpl is null, so "buildUniqueKeyFromColumnNames" is never called.

As ImplicitNamingStrategy#determineUniqueKeyName has no JavaDoc, how should it be used and when should it be called?
Is it a bug that it's not called for native HBM-Mappings?
If I can't use the NamingStrategy for this, what's the recommended way to preserve the unique key constraints?

Thanks,
Holger


Top
 Profile  
 
 Post subject: Re: Single-column unique constraint names in Hibernate 3 and 5
PostPosted: Tue Sep 13, 2016 4:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
For Hibernate 5, you can put a breakpoint in ImplicitNamingStrategyJpaCompliantImpl#determineUniqueKeyName since that's where the unique constraints are being generated.

Anyway, what is the actual problem that you have? Are you using hbm2ddl for your system? While hbm2ddl is good for the initial schema, you might want to use FlywayDB since it's way more advanced than hbm2ddl anyway.


Top
 Profile  
 
 Post subject: Re: Single-column unique constraint names in Hibernate 3 and 5
PostPosted: Tue Sep 13, 2016 5:30 am 
Beginner
Beginner

Joined: Tue Dec 26, 2006 3:50 pm
Posts: 22
Unfortunately, this is not the case.
Even Hibernate 5 seems to generate the constraint name here:
Code:
   Constraint.generateName(String, Table, Column...) line: 57   
   StandardTableExporter.getSqlCreateStrings(Table, Metadata) line: 113   
   StandardTableExporter.getSqlCreateStrings(Exportable, Metadata) line: 30   
   SchemaCreatorImpl.doCreation(Metadata, boolean, Dialect, Target...) line: 230   
   SchemaCreatorImpl.doCreation(Metadata, boolean, Target...) line: 128   
   SchemaExport.<init>(ConnectionHelper, ServiceRegistry, MetadataImplementor, boolean) line: 199   
   SchemaExport.<init>(ServiceRegistry, MetadataImplementor, boolean) line: 140   
   SessionFactoryImpl.<init>(MetadataImplementor, SessionFactoryOptions) line: 465   
   SessionFactoryBuilderImpl.build() line: 444   
   Configuration.buildSessionFactory(ServiceRegistry) line: 708   
...


Stacktrace is for version 5.0.10.
So according to your own expectations, this seems to be an oversight?

With regard to using hbm2ddl vs. FlywayDB:
When developers create a new test DB we currently use the schema generated by Hibernate.
Also for testing with in-memory DB we use this feature.
Obviously, the generated schema should be identical to the existing PROD schema.


Top
 Profile  
 
 Post subject: Re: Single-column unique constraint names in Hibernate 3 and 5
PostPosted: Tue Sep 13, 2016 5:56 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You don't really need to use an in-memory DB during testing. You can run any RDBMS almost as fast as HSQLDB or H2 using an in-memory disk.

As for the mapping issue, you can open a Jira issue for it. Make sure that you provide a replicating test case as well. Annotations have better support than HBM in recent versions of Hibernate, so you might want to consider migrating to annotations as well. In a future version of Hibernate, HBM support might be even dropped in favor for the JPA XML mappings + some extension mechanism.


Top
 Profile  
 
 Post subject: Re: Single-column unique constraint names in Hibernate 3 and 5
PostPosted: Wed Sep 14, 2016 2:53 am 
Beginner
Beginner

Joined: Tue Dec 26, 2006 3:50 pm
Posts: 22
Yes, but the point here was to initialize the DB with an initial schema, be it H2 or PostgreSQL or whatever.

Ok, here is the issue about the ImplicitNamingStrategy not used consequently throughout the codebase (at least for unique key names):
https://hibernate.atlassian.net/browse/HHH-11103


Top
 Profile  
 
 Post subject: Re: Single-column unique constraint names in Hibernate 3 and 5
PostPosted: Wed Sep 14, 2016 3:03 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Thanks for creating the Jira ticket.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.