Hibernate的JavaBean属性名称大小写的一个Bug

来源:互联网 发布:网络与信息安全就业 编辑:程序博客网 时间:2024/04/30 07:23

      Hibernate的JavaBean的属性名称,如果第一个字母小写,第二个字母大写,则通过属性名称获取值,会出现小小的问题。

如Bean类名为BeanA,属性名称为fIELD(get/set方法则按照属性名对应设置,如getFIELD/setFIELD),通常情况下,我们会通过类似a.fIELD获取属性,但实际上如果在Hibernate中通过这样获取属性,如HQL语句:from BeanA a where a.fIELD = ?,则会提示fIELD属性找不到,而通过a.FIELD,则正确。把属性的第二个字母改为小写,get/set方法相应的设置为getFiELD/setFiELD,则通过a.fiELD获取属性,无任何问题。

     通过网上搜索,有人说是Hibernate的一个bug,以下为该文的转帖,正确与否,未做细究。

JavaBean:属性名大小写与getter方法命名问题

情况如下,Company类的对象有一个叫sAddress的属性,根据JavaBean Specification,getter和setter分别为getSAddress和setSAddress。但是编译时会报错:

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的诱因。

那么,解决方法有三种:

  1. 把属性名改成SAddress,这样就满足上面匹配判断的第二个条件(方法名截掉“get”后,与属性名匹配)。但是,这样做不符合Java命名规范;
  2. 把getSAddress()改成getsAddress(),这样也满足上面匹配判断的第二个条件(方法名截掉“get”后,与属性名匹配)。但是,这样做不符合JavaBean命名规范;
  3. 把属性名改成strAddress,并形成一种约定:命名属性时,第二个字符只能是小写字母。这个方法不需要做更多地修改,符合所有规范,最为稳妥。
原创粉丝点击