DuplicateSecondaryTableException: Table with that name [Cluster] already associated with entity

We are trying to migrate our application from hibernate 3 to hibernate 5 (From 3.6.10.Final to 5.1.10.Final to be specific).

I was able to resolve many compilation issues.

But application start fails with the following error.

Invocation of init method failed; nested exception is org.hibernate.boot.spi.InFlightMetadataCollector$DuplicateSecondaryTableException: Table with that name [Cluster] already associated with entity
                at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:592)
                at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
                at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
                at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
                at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
                at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
                at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:754)
                at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
                at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
                at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
                at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)

NOTE:
I have 2 schema schema1 & schema2 Cluster entity needs to encapsulate both. Some columns from schema1 and some columns from schema2.
There are no changes in table structure. just hibernate upgrade.
@org.hibernate.annotations.Entity(optimisticLock = OptimisticLockType.DIRTY, dynamicUpdate = true)
is replaced with
@Entity
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@DynamicUpdate
The below is the entity with used annotations.

Used in hibernate 3

@GenerateMutator
@Table(name = “cluster”, schema = “schema1”)
@SecondaryTable(name = “Cluster”, schema = “schema2”, pkJoinColumns = { @PrimaryKeyJoinColumn(name = “id”) })
@org.hibernate.annotations.Table(appliesTo = “Cluster”, optional = false)
@Entity
@org.hibernate.annotations.Entity(optimisticLock = OptimisticLockType.DIRTY, dynamicUpdate = true)
public class Cluster

Modified in hibernate 5

@GenerateMutator
@Table(name = “cluster”, schema = “C”)
@SecondaryTable(name = “cluster”, schema = “schema2”, pkJoinColumns = { @PrimaryKeyJoinColumn(name = “id”) })
@org.hibernate.annotations.Table(appliesTo = “Cluster”, optional = false)
@Entity
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@DynamicUpdate
public class Cluster

Can some one guide me on what could be the issue and any solution for this ?

Why does in Hibernate 3 you used “schema1” while in 5 you use “C”?

And, why does the org.hibernate.annotations.Table appliedTo is Cluster while the table name is cluster?

Also, it might be that Hibernate 5 does not take into consideration the schema when comparing the primary and secondary table names, so it thinks it’s a duplicate.

If you can replicate it with a test case, you should open a Jira issue.

Sorry that was by mistake, in hibernate 5 as well i am using schema1.

org.hibernate.annotations.Table appliedTo is name of the targeted table, isn’t it case sensitive ?
in Hibernate 3 it was working just like that. should it be table name(in all small letter ?).
I will try modifying it by making it all small case.

“Also, it might be that Hibernate 5 does not take into consideration the schema when comparing the primary and secondary table names, so it thinks it’s a duplicate.”
In hibernate 3 working code, schema was not present, however i tried adding it while upgrading to hibernate5, but the error remained the same, no change with and without schema.

I will try it with test case(link provided). it might give more info.

I tried the code in hibernate 4 and hibernate 5 with the help of template which you have provided.

  • With same entity definition(using schema in SecondaryTable) it is working in hibernate 4 and failing in hibernate 5

  • If i use schema with different table name it works. Ex. table name in schema1 and schema2 are different.

But unfortunately we are not in a position to change table name in any of the schema as there are many tables and also they are exposed to our customers.
As per your advise, i raised a Jira ticket.
https://hibernate.atlassian.net/browse/HHH-12423

Is there a workaround for this(Can’t change the table name) ? or i am blocked ?

annotation

I don’t think there’s any workaround other than fixing the issue.

Try to provide a fix as well via a Pull Request.

1 Like

Did it got resolved ?