Patrik Beck
Patrik Beck

Reputation: 2505

Getting JPA to generate tables with specified row format

I am using play 2 framework with JPA hibernate. My goal is to get JPA to generate tables on a fresh database using specific ROW_FORMAT:

This is what JPA executes on start:

create table Admin (
  ..
) ENGINE=InnoDB

And this is what I want:

create table Admin (
  ..
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC

The persistance.xml looks like this:

<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>DefaultDS</non-jta-data-source>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>

I tried looking for ways to use annotations in the java code, or a specific property in for the XML conf. I had no luck.

Upvotes: 2

Views: 817

Answers (1)

Alan Hay
Alan Hay

Reputation: 23226

You can do it by subclassing MySQL5InnoDBDialect and overriding the getTableTypeString() method as below.

public class MySQL5CustomInnoDBDialect extends MySQL5InnoDBDialect{

    /**
     * Call to super returns " type=innodb". 'Type' is a 
     * deprecated MySQL synonym for 'engine'         
     * so you may want to ignore the super call and use:
     * " engine=innodb ROW_FORMAT=DYNAMIC"       
     */
    public String getTableTypeString() {
        return super.getTableTypeString() + " ROW_FORMAT=DYNAMIC";
    }   
}

And obviously set this as your dialect:

<property name="hibernate.dialect" value="org.foo.MySQL5CustomInnoDBDialect"/>

Upvotes: 3

Related Questions