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?
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:
NamingStrategy
(Hibernate Reference)NamingStrategy
(Hibernate JavaDocs)CaseFormat
(Guava JavaDoc)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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With