How to set up a different Naming Strategy

720 views
Skip to first unread message

Marc van Kempen

unread,
Sep 13, 2010, 2:00:16 PM9/13/10
to play-framework
Hi,

I've just started playing with the Play framework and so far I like it
a lot! However I could not figure out how to set the hibernate naming
strategy. I would like the field names for my tables to have
underscores to separate the different words, e.g.

public String muscleFocus

would become

muscle_focus

Hibernate has a naming strategy that already does this (so I don't
need to implement my own for now), however looking at the code there
is no support for setting it in the configuration file.

I could add it to the play.db.jpa.JPAPlugin class where it sets the
different JPA options from application.conf.

Would adding a new property 'hibernate.naming_strategy' be the
preferred way to do this? i.e. setting it would mean adding the
following property to application.conf

hibernate.naming_strategy=net.sf.hibernate.cfg.ImprovedNamingStrategy

Please advice and I will provide patches.

Kind regards,
Marc van Kempen.

Guillaume Bort

unread,
Sep 13, 2010, 3:54:20 PM9/13/10
to play-fr...@googlegroups.com
You can alreadt add any hibernate.* configuration properties to
application.conf

Le 13 sept. 2010 à 20:00, Marc van Kempen <m.van...@gmail.com> a
écrit :

> --
> You received this message because you are subscribed to the Google
> Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en
> .
>

Marc van Kempen

unread,
Sep 14, 2010, 4:12:43 PM9/14/10
to play-framework
Aha, I see, I missed that, should be in the documentation!

However, as far as I can tell, but maybe I'm missing something,
setting a new NamingStrategy requires calling the setNamingStrategy()
on the Ejb3Configuration object with an instance of the specified
class. This will not work with the property setting mechanism you
mentioned.

Kind regards,
Marc.

On Sep 13, 9:54 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> You can alreadt add any hibernate.* configuration properties to  
> application.conf
>
> Le 13 sept. 2010 à 20:00, Marc van Kempen <m.vankem...@gmail.com> a  

Guillaume Bort

unread,
Sep 14, 2010, 5:34:11 PM9/14/10
to play-fr...@googlegroups.com
Are you sure? I think that you can provide a class name via properties
configuration.

Le 14 sept. 2010 à 22:12, Marc van Kempen <m.van...@gmail.com> a
écrit :

> Aha, I see, I missed that, should be in the documentation!

>>> fram...@googlegroups.com.

Marc van Kempen

unread,
Sep 15, 2010, 5:37:20 AM9/15/10
to play-framework
You are right, however this is extremely badly documented on
Hibernate's part, e.g. these documents do not mention this possibility
at all

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/session-configuration.html#configuration-optional
and
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/session-configuration.html#configuration-namingstrategy

I had to go look at the source for Ejb3Configuration to see the
possibilities.

The solution is as follows, add the following line to application.conf
to configure the NamingStrategy

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

Make sure that the class has a default constructor, a slightly better
version of the default naming strategy is the bundled class:

org.hibernate.cfg.ImprovedNamingStrategy

How do I go about updating at least the play documentation?

Kind regards,
Marc.

On Sep 14, 11:34 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Are you sure? I think that you can provide a class name via properties  
> configuration.
>
> Le 14 sept. 2010 à 22:12, Marc van Kempen <m.vankem...@gmail.com> a  

GrailsDeveloper

unread,
Sep 15, 2010, 6:50:29 AM9/15/10
to play-framework
I would document it first at http://stackoverflow.com. There it could
be easily founded and other people can add comments. Furthermore it
general interesting for people how use hibernate.
Then I would add comment at http://www.playframework.org/documentation/1.0/jpa
which links to the stackoverflow.com.
At least I would create bug report with the tag documentation and a
link to stackoverflow.

Niels

On 15 Sep., 11:37, Marc van Kempen <m.vankem...@gmail.com> wrote:
> You are right, however this is extremely badly documented on
> Hibernate's part, e.g. these documents do not mention this possibility
> at all
>
>  http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/session...
> and
>  http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/session...

Sean Van Buggenum

unread,
Jan 7, 2016, 10:31:20 AM1/7/16
to play-framework, openso...@googlemail.com
It's been a while since this thread has been touched, 
but i tried to use this today, and it didn't work.
Since Hibernate 5, I believe, things work differently. 

Now, I need to use the following:

<property name="hibernate.physical_naming_strategy" value="{your naming strategy here!!!}" />

Additionally, you will unfortunately probably have to provide your own namingstrategy, as there don't seem to be any compatible strategy classes anymore which convert camel case into underscore separated words. 
That is pretty easy, but annoying if you preferred not to have any persistence library specific (i.e., Hibernate) code in your beautiful pure JPA play application ;)

extend PhysicalNamingStrategyStandardImpl and improve what you like. 

oh well. 
Reply all
Reply to author
Forward
0 new messages