Solving the eclipselink (former toplink) issue with HSQLDB

When using the great hypersonic java database (hsql) with eclipselink 1.0, i stumbled upon a schema generation (ddl generation) issue. It appears as though eclipselink generates “CREATE TABLE…” statements containing UNIQUE keywords which are incompatible with HSQLDB.
As it turned out, there already is an issue filed for the problem (240618).

The resulting exceptions look like this:


[EL Warning]: 2008.08.12 11:01:13.134--ServerSession(28607378)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 1.0 (Build 1.0 - 20080707)): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Unexpected token: UNIQUE in statement [CREATE TABLE ... (ID INTEGER UNIQUE]
Error Code: -11


Jason Drake, who reported the issue for toplink, suggests modifying the FieldDefinition and DefaultTableGenerator. (The error migrated from toplink into eclipselink)

As a simpler alternative, one might just subclass the HSQLPlatform class and override the supportsUniqueConstraint and printFieldUnique methods. This has worked for my testsuite, which i migrated from hibernate to eclipselink.

Here is my subclassed HSQLPlatform implementation:

package my.somepackage
/**
 * This is a workaround HSQL DB platform implementation which omits unique constraints
 * since they are not supported in the syntax that eclipselink is using.<br />
 * See: <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=240618">issue 240618</a>.
 *
 * @author Olaf Otto
 * @see org.eclipse.persistence.platform.database.HSQLPlatform
 */
public class HsqlDbPlatform extends HSQLPlatform {
    /**
     * Workaround for  https://bugs.eclipse.org/bugs/show_bug.cgi?id=240618.
     *
     * @return <code>false</code>.
     */
    @Override
    public boolean supportsUniqueKeyConstraints() {
        return false;
    }

    /**
     * Workaround for  https://bugs.eclipse.org/bugs/show_bug.cgi?id=240618.
     * <br />
     * Does nothing.
     */
    @Override
    public void printFieldUnique(Writer writer, boolean shouldPrintFieldIdentityClause) throws IOException {
        // Do nothing.
    }
}

BTW: Eclipselink is already supported in Spring 2.5.2. If you use spring, providing the customized database platform is a simple property change in the jpa vendor adapter:

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
        <property name="generateDdl" value="true"/>
        <property name="showSql" value="false"/>
        <property name="databasePlatform" value="my.somepackage.HsqlDbPlatform"/>
    </bean>

This entry was posted on Tuesday, August 12th, 2008 at 10:53. Posted in: J2EE, JPA, eclipselink, java. You can follow any responses to this entry through the RSS 2.0feed. You can leave a response, or trackback from your own site.

Leave a Reply