JPA and table prefix


JPA is great but it doesn’t manage a standard way to add a prefix to database tables out of the box.

All (known) vendors have some hooks to do so but that’s not obvious or specified. For hibernate you have to play with NamingStrategy, with OpenJPA you either define a custom DBDictionary or a NamingDefaults and with EclipseLink you have to implement a SessionCustomizer and play with eclipselink model.

That’s why in OpenEJB and TomEE we started to work on it.

What we expect is to simply define a string in a good place to prefix all tables. It sounds logical to consider as a good place the persistence unit and more particularly the place where we define vendor specific properties: the properties tag.

The “magic” property name is openejb.jpa.table_prefix.

So for instance you can define:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                       http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="demo">
    <jta-data-source>jdbc/demo</jta-data-source>
    <class>org.superbiz.Demo</class>
    <properties>
      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
      <property name="openejb.jpa.table_prefix" value="DEMO_"/>
    </properties>
  </persistence-unit>
</persistence>

Then it should prefix your tables.

Note: that’s an in progress feature. If you encounter any issue with it please report it on OpenEJB mailing list (users@openejb.apache.org).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s