Changing property names in JPA requests

I use the underscore prefixing field naming convention. When I create classes of annotated entities with such fields, I am stuck in using property names with prefix-prefix in queries. I want to avoid this and be able to:

@Entity public class Container { private String _value; } // in a lookup method executeQuery("from Container where value = ?", value); 

Is this possible with JPA in general or Hibernate in particular?


Update: Trying to remember why, but I need it to be annotated by fields, not by getters.

+4
source share
3 answers

You can annotate a getter:

 @Entity public class Container { private String _value; @Column public String getValue() { return _value; } public void setValue( String value ) { this._value = value; } } 
+4
source

Perhaps you could subclass your generated entity classes that have getter methods, and then configure the entity manager to use getter / setter access if this is an access field? Then your getters / setters could have any name you like.

+1
source

Check out NamingStrategy . It would be fairly easy to extend DefaultNamingStrategy and override the columnName method to break the first underscore (if any).

0
source

All Articles