Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for sAddress in class Company at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:282)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:275)
跟蹤到org.hibernate.property.BasicPropertyAccessor類(lèi)中的getterMethod(Class theClass, String propertyName)方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | private static Method getterMethod(Class theClass, String propertyName) { Method[] methods = theClass.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { // only carry on if the method has no parameters if (methods[i].getParameterTypes().length == 0) { String methodName = methods[i].getName(); // try "get" 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]; } } // if not "get" then try "is" if (methodName.startsWith("is")) { String testStdMethod = Introspector.decapitalize(methodName.substring(2)); String testOldMethod = methodName.substring(2); if (testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName)) { return methods[i]; } } } } return null; } |
getterMethod()對Company類(lèi)中聲明的方法進(jìn)行遍歷,找到與屬性名匹配的方法,即屬性的getter方法。比較分兩部分,第一部分,針對primitive和自定義類(lèi)類(lèi)型的屬性;第二部分,針對boolean類(lèi)型的屬性(由于boolean類(lèi)型屬性的getter方法的特殊性)。
跟蹤發(fā)現,methodName的值為“getSAddress”,propertyName的值為“sAddress”,testOldMethod的值為“SAddress”,testStdMethod的值為“SAddress”。testStdMethod和testOldMethod相同,而它們都不匹配propertyName!
因此,getterMethod()中找不到與屬性sAddress匹配的getter方法,getterMethod()返回null,導致異常。
問(wèn)題出在Introspector.decapitalize()方法。
decapitalize()源碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /** * Utility method to take a string and convert it to normal Java variable * name capitalization. This normally means converting the first * character from upper case to lower case, 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. * * Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays * as "URL". * * @param name The string to be decapitalized. * @return The decapitalized version of the string. */ public static String decapitalize(String name) { if (name == null || name.length() == 0) { return name; } if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) && Character.isUpperCase(name.charAt(0))){ return name; } char chars[] = name.toCharArray(); chars[0] = Character.toLowerCase(chars[0]); return new String(chars); } |
注釋說(shuō)了:一般情況下,把字符串第一個(gè)字母變?yōu)樾?xiě),如把“FooBah”變?yōu)?#8220;fooBah”。但在特殊情況下,即字符串前兩個(gè)字母都是大寫(xiě)的時(shí)候,什么也不做,如,遇到“URL”,原樣返回。
decapitalize()的bug是:如果一個(gè)字符串,前兩個(gè)字母大寫(xiě),但后面還有小寫(xiě)字母,它仍然返回原字符串!
Hibernate的開(kāi)發(fā)者注意到decapitalize()的特點(diǎn),所以才在判斷語(yǔ)句中使用一個(gè)或運算(不然只需要判斷方法名截掉“get”,再改第一個(gè)字母為小寫(xiě)后的字符串與屬性名是否相等即可,這也是按照JavaBean Specification定義的標準做法)。但是,Hibernate沒(méi)有解決這個(gè)bug,可能是他們沒(méi)有碰到我遇到的情況。
類(lèi)似sAddress(一般性地說(shuō),第一個(gè)字母小寫(xiě),第二個(gè)字母大寫(xiě))屬性命名就是bug的誘因。
那么,解決方法有三種:
- 把屬性名改成SAddress,這樣就滿(mǎn)足上面匹配判斷的第二個(gè)條件(方法名截掉“get”后,與屬性名匹配)。但是,這樣做不符合Java命名規范;
- 把getSAddress()改成getsAddress(),這樣也滿(mǎn)足上面匹配判斷的第二個(gè)條件(方法名截掉“get”后,與屬性名匹配)。但是,這樣做不符合JavaBean命名規范;
- 把屬性名改成strAddress,并形成一種約定:命名屬性時(shí),第二個(gè)字符只能是小寫(xiě)字母。這個(gè)方法不需要做更多地修改,符合所有規范,最為穩妥。

