Sunday, 21 November 2010

JPA Field Names Break Encapsulation

I've made this mistake a couple of times.

When writing JPA queries I must remember to use the private field name not the bean property name.

This is annoying and leads to mistakes writing setters. I usually prefix field names with and underscore "_" or "m" (depending on the convention in the code base). If I don't do this I sometimes end up assigning the field to itself in the setter.

This detail has to be exposed to the JPA. So I tend to name the field without a prefix in JPA persistent classes.

I suppose I could always do query by example or some JPA annotation to hide the difference.

No comments:

Post a Comment