views:

133

answers:

1

Is there a generic way to tell JPA/Hibernate to transform all column names of the form:

emailAddress

to the form:

email_address

?

I'd rather not have to use a @Column annotation for hundreds of columns. I hope someone thought of this ;)

+2  A: 

See the Hibernate Reference Documenation - 3.6. Implementing a NamingStrategy

The interface org.hibernate.cfg.NamingStrategy allows you to specify a "naming standard" for database objects and schema elements.

You can provide rules for automatically generating database identifiers from Java identifiers or for processing "logical" column and table names given in the mapping file into "physical" table and column names. This feature helps reduce the verbosity of the mapping document, eliminating repetitive noise (TBL_ prefixes, for example). The default strategy used by Hibernate is quite minimal. You can specify a different strategy by calling Configuration.setNamingStrategy() before adding mappings: ...

See also: http://www.javalobby.org/java/forums/t19838.html

Ash Kim
Thanks for the response. I've tried this, but it doesn't work with JPA. I put println statements to see if the naming strategy is getting called... and it doesn't. And yes, in the persistence.xml, I am setting hibernate's naming strategy property to the custom implementation of naming strategy :(
egervari
Nevermind, the real problem was that I needed to use "hibernate.ejb.naming_strategy" instead of "hibernate.naming.strategy". Grrrr. Thanks
egervari