Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get Play framework and JPA to use underscores in column names?

I'm trying to build a Play! app running against an existing database where all the columns have underscores to separate words. This means I have to put an @Column annotation on each field to specify a different name. Is there anyway to get Play! to use underscores by default?

like image 548
Aaron Avatar asked Apr 26 '11 21:04

Aaron


2 Answers

If Play uses Hibernate, as the other answers suggest, you will have to implement a custom NamingStrategy.

Here's a sample NamingStrategy that converts all column names from lower camel to lower case 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);
    }
}

Configure it like this:

add the following line to application.conf to configure the NamingStrategy

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

Reference:

like image 72
Sean Patrick Floyd Avatar answered Sep 29 '22 20:09

Sean Patrick Floyd


The solution given works great, but as stated in the comments, ImprovedNamingStrategy does that, so there's no need to use Guava.

An improved naming strategy that prefers embedded underscores to mixed case names

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

like image 29
Baztoune Avatar answered Sep 29 '22 20:09

Baztoune