I'm using a convention of prefixing field names with an underscore. When I generate annotate entity classes with such fields I am stuck to using the underscore-prefixed property names in queries. I want to avoid that, and be able to do:
我正在使用带有下划线的字段名前缀的约定。当我生成带有这些字段的注释实体类时,我坚持在查询中使用下划线前缀属性名称。我想避免这种情况,并且能够做到:
@Entity
public class Container {
private String _value;
}
// in a lookup method
executeQuery("from Container where value = ?", value);
Is that possible with JPA in general or Hibernate in particular?
这是可能的JPA一般或特别是Hibernate?
Update: Still trying to remember why, but I need this to be annotated on fields rather than on getters.
更新:仍然试图记住原因,但我需要在字段上注释而不是在getter上注释。
3 个解决方案
#1
You can annotate the getter:
你可以注释getter:
@Entity
public class Container {
private String _value;
@Column
public String getValue()
{
return _value;
}
public void setValue( String value )
{
this._value = value;
}
}
#2
You could perhaps write subclasses of your generated entity classes, which have getter methods on them, and then configure the entity manager to use getter/setter access instead if field access? Then your getters/setters could have any name you liked.
您也许可以编写生成的实体类的子类,它们上面有getter方法,然后将实体管理器配置为使用getter / setter访问而不是字段访问?那么你的getter / setter可以有你喜欢的任何名字。
#3
Have a look at NamingStrategy
. It would be fairly easy to extend the DefaultNamingStrategy
and override the columnName
method to strip the first underscore (if it is there).
看看NamingStrategy。扩展DefaultNamingStrategy并覆盖columnName方法以去除第一个下划线(如果它在那里)将相当容易。
#1
You can annotate the getter:
你可以注释getter:
@Entity
public class Container {
private String _value;
@Column
public String getValue()
{
return _value;
}
public void setValue( String value )
{
this._value = value;
}
}
#2
You could perhaps write subclasses of your generated entity classes, which have getter methods on them, and then configure the entity manager to use getter/setter access instead if field access? Then your getters/setters could have any name you liked.
您也许可以编写生成的实体类的子类,它们上面有getter方法,然后将实体管理器配置为使用getter / setter访问而不是字段访问?那么你的getter / setter可以有你喜欢的任何名字。
#3
Have a look at NamingStrategy
. It would be fairly easy to extend the DefaultNamingStrategy
and override the columnName
method to strip the first underscore (if it is there).
看看NamingStrategy。扩展DefaultNamingStrategy并覆盖columnName方法以去除第一个下划线(如果它在那里)将相当容易。