"SchemaManagementException: SQL strings added more than once" in Hibernate

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 7
  • Hibernate 5

Issue

  • Two different entities in a single persistence unit are mapped to tables by the same name which reside in different database schemas

  • The tables each have unique constraints on identically named, non-primary key properties

        @Entity
        @Table(name = "employee", schema = "testone")
        public class EmployeeOne {
                @Id
                private String name;
    
                @Column(unique = true)
                private String title;
                ...
        }
    
        @Entity
        @Table(name = "employee", schema = "testtwo")
        public class EmployeeTwo {
                @Id
                private String name;
    
                @Column(unique = true)
                private String title;
                ...
        }
    
  • When Hibernate is used to generate the database artifacts, (e.g. hibernate.hbm2ddl.auto=create-drop), an exception is raised

    ... org.hibernate.tool.schema.spi.SchemaManagementException: SQL strings added more than once for: employee.UK-UK_....
        at org.hibernate.tool.schema.internal.SchemaCreatorImpl.checkExportIdentifier(SchemaCreatorImpl.java:408)
        at org.hibernate.tool.schema.internal.SchemaCreatorImpl.createFromMetadata(SchemaCreatorImpl.java:347)
        at org.hibernate.tool.schema.internal.SchemaCreatorImpl.performCreation(SchemaCreatorImpl.java:165)
        at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:134)
        at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:120)
        at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:148)
        at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:65)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:478)
        at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:423)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:880)
        ...
    

Resolution

This issue1 is resolved in This content is not included.EAP 7.1 cumulative patch (CP) 6 and later releases. Production systems should use the latest available cumulative patch.

Root Cause

This is a known defect (Content from hibernate.atlassian.net is not included.HHH-12935).

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.