hibernate 特殊情况下 属性找不到get方法的解决方案

来源:互联网 发布:属于淘宝禁售商品刀 编辑:程序博客网 时间:2024/04/27 22:58

Caused by: org.hibernate.PropertyNotFoundException: Could not find a getterfor pOrgID in class com.oarage.basedata.commons.entity.UnloadOil

 

我说的情况是属性名称第一个字母小写,第二个字母大写,例如:eTypeName,则按照javabean specification规定的getter方法为getETypeName,则会出现找不到getter方法的错误。
今天看了一下javabean specification,规定了一般的属性名称首字母小写,如果遇到前两个字母大写的属性,则getter方法保持属性名称不变,例如URL属性的getter方法为getterURL,但是并没有规定类似eTypeName这样的属性的getter方法写成geteTypeName的形式。

 

 

如果Hibernate实体属性名称 第2个字母是大写 或者 出现两个字母大写:

hibernate 3.2.5.ga中会出现错误:Could not find a getter for “fieldName

hibernate3.2.7中会出现错误: Unable to instantiate default tuplizer



Hibernate用的方法是 :BasicPropertyAccessor.java

String methodName = methods[i].getName();
if( methodName.startsWith("get") ) {
        String testStdMethod = Introspector.decapitalize(methodName.substring(3) );
        String testOldMethod = methodName.substring(3);
        if( testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName) ) return methods[i];       
}

调用的是 :Introspector.decapitalize(String name);
Sun的注释说明
but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone.
他得到getXSize这个方法名,substring[3]为XSize然后decapitalize之后不修改首位字符,就Could not find a getter for xSize了
这个是Java Bean的Spec里面规定的。方法有多种,你可以写成xsize这种全部小写,或者加"_"字符,如果你首位大写不在第二位就没问题了。


另外出现Unable to instantiate default tuplizer错误可能的原因:
是某个实体类中外加了某些getXX(),而不存在XX属性,这时要在get上加@Transient,不持久 化它就行了



参考:
http://blog.163.com/ace_ning/blog/static/1208726912009612815294/ ,hibernate实体类属性第二个字母大写Could not find a getter for问题
http://blog.csdn.net/jjss2006/archive/2009/08/21/4470702.aspx,异常:Unable to instantiate default tuplizer

原创粉丝点击