Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL strings added more than once for: tablename

Tags:

java

hibernate

I'm bootstrapping a database using hibernate-maven-plugin, using models that it scans in the maven module it's executed in.

Unfortunately, it stops when hibernate throws this:

org.hibernate.tool.schema.spi.SchemaManagementException: SQL strings added more than once for: reference_data_source.UK-UK_9ec6wdvyj3mjagiptcnrq2txv
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.checkExportIdentifier(SchemaCreatorImpl.java:299)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:255)
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:128)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:199)

So, I have two persistence units, and some tables exists in both. Hibernate seems to interpret this like the same table though, so when it tries to store the same index, but for another schema, it fails thinking it is a duplicate. Their code can be found here.

I'm not sure how to approach this, anyway to configure hibernate hbm2ddl to keep track of these different peristence units?

This is the configuration for the hibernate-maven-plugin:

<plugin>
    <groupId>de.juplo</groupId>
    <artifactId>hibernate-maven-plugin</artifactId>
    <version>2.0.0</version>
    <configuration>
        <detail>true</detail>
        <persistenceUnit>mainPersistenceUnit</persistenceUnit>
        <driver>com.mysql.jdbc.Driver</driver>
        <dialect>org.hibernate.dialect.MySQL5Dialect</dialect>
        <force>true</force>
        <url><![CDATA[jdbc:mysql://localhost/auto_bootstrap_schema]]></url>
        <username>user</username>
        <password>pass</password>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>create</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
        </dependency>
    </dependencies>
</plugin>
like image 373
Wrench Avatar asked Nov 30 '22 15:11

Wrench


2 Answers

I had the same problem. The reason in my case was that I had three entities starting with same prefix name and a OneToMany relation to the each more specialized one:

Person
PersonCard
PersonCardLayout

Renaming my model to this solved my problem:

Person
Card
Layout

This seems to be a bug in Hibernate.

like image 56
Andreas Hof Avatar answered Dec 05 '22 10:12

Andreas Hof


i have the same problem, looks like a bug in: org.hibernate.mapping.UniqueKey#getExportIdentifier

@Override
public String getExportIdentifier() {
    return StringHelper.qualify( getTable().getName(), "UK-" + getName() );
}

because the identifier was build only from table name but without schema/catalog of the table.

Therefore, if you have to entities with same table name but different schema and in these entities two properties with same name and "unique = true" you will drop into the bug.

like image 30
Markus Schulz Avatar answered Dec 05 '22 10:12

Markus Schulz