Is there a way to force the Play framework and JPA to use underscores in column names?

I'm trying to create a play! the application works with an existing database, where all columns have underscores for word separation. This means that I have to put the @Column annotation in each field to indicate a different name. There is no matter to get Play! use default underscores?

+8
java hibernate jpa playframework
source share
3 answers

If Play uses Hibernate, as the other answers indicate, you need to implement custom NamingStrategy .

Here's a NamingStrategy sample that converts all column names from lower camel to lowercase with underscores using Guava :

 public class CustomNamingStrategy extends ImprovedNamingStrategy { private static final long serialVersionUID = -306957679456120781L; @Override public String columnName(final String columnName) { return CaseFormat.LOWER_CAMEL .to(CaseFormat.LOWER_UNDERSCORE, columnName); } } 

Set it up like this:

add the following line to application.conf to configure NamingStrategy

hibernate.ejb.naming_strategy=<your naming strategy classname>

Reference:

+14
source share

The above solution works fine, but as indicated in the comments, ImprovedNamingStrategy does this, so there is no need to use Guava.

Improved naming strategy that prefers built-in underscores for mixed case names

You can simply add hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy in application.conf (tested in Play! Framework 1.2.5)

+1
source share

Well, if I read the document correctly, the playback platform uses Hibernate as its JPA implementation. AFAIK, there’s no way to change the default Hibernate naming convention, so you’ll need to add annotations or provide column names in the mapping XML file.

0
source share

All Articles