March 29, 2011 | Category: Uncategorized

JPA and Bad Assumptions

I made a bad assumption about how JPA (with Hibernate) works a while ago, that came back to bite me recently. For a long time, I’d assumed that if you have an @Column annotation with a name attribute, that name would always be the name that gets used to match the field to a DB column i.e. @Column(name="someName") would always resolve to the DB column “someName”.

I was very much mistaken in that assumption.

When using Hibernate (and possibly other JPA implementations, I’ll need to check), it turns out that name just replaces the field name as it goes through the normal renaming process. That is, the name is still subject to all of Hibernate’s naming strategy code.

By default, that means that when using the ImprovedNamingStrategy (as most people are), “someName” becomes “some_name”. This is definitely a more db-like name, but took me a little by surprise when I saw this happening in someone’s code.

The lesson: never assume anything.