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类中的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类中声明的方法进行遍历,找到与属性名匹配的方法,即属性的getter方法。比较分两部分,第一部分,针对primitive和自定义类类型的属性;第二部分,针对boolean类型的属性(由于boolean类型属性的getter方法的特殊性)。
跟踪发现,methodName的值为“getSAddress”,propertyName的值为“sAddress”,testOldMethod的值为“SAddress”,testStdMethod的值为“SAddress”。testStdMethod和testOldMethod相同,而它们都不匹配propertyName!
因此,getterMethod()中找不到与属性sAddress匹配的getter方法,getterMethod()返回null,导致异常。
问题出在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); } |
注释说了:一般情况下,把字符串第一个字母变为小写,如把“FooBah”变为“fooBah”。但在特殊情况下,即字符串前两个字母都是大写的时候,什么也不做,如,遇到“URL”,原样返回。
decapitalize()的bug是:如果一个字符串,前两个字母大写,但后面还有小写字母,它仍然返回原字符串!
Hibernate的开发者注意到decapitalize()的特点,所以才在判断语句中使用一个或运算(不然只需要判断方法名截掉“get”,再改第一个字母为小写后的字符串与属性名是否相等即可,这也是按照JavaBean Specification定义的标准做法)。但是,Hibernate没有解决这个bug,可能是他们没有碰到我遇到的情况。
类似sAddress(一般性地说,第一个字母小写,第二个字母大写)属性命名就是bug的诱因。
那么,解决方法有三种:
- 把属性名改成SAddress,这样就满足上面匹配判断的第二个条件(方法名截掉“get”后,与属性名匹配)。但是,这样做不符合Java命名规范;
- 把getSAddress()改成getsAddress(),这样也满足上面匹配判断的第二个条件(方法名截掉“get”后,与属性名匹配)。但是,这样做不符合JavaBean命名规范;
- 把属性名改成strAddress,并形成一种约定:命名属性时,第二个字符只能是小写字母。这个方法不需要做更多地修改,符合所有规范,最为稳妥。