From: Guilherme R. (JIRA) <no...@at...> - 2006-05-12 19:01:10
|
Issue choosing accessor method ------------------------------ Key: HHH-1747 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1747 Project: Hibernate3 Type: Bug Components: core Versions: 3.1.3 Environment: Hibernate 3.1.3, Postgresql 8.0 (but bug should be database-independent). Reporter: Guilherme Rios Priority: Minor Suppose you have a Hibernate-managed POJO that has an interface like... ---------- public void setFlag(Integer flag); public Integer getFlag(); public Boolean isFlag(); ---------- The idea here is, for whatever reason, one chooses to persist a field as an Integer, but sometimes it will be more convenient for the application to retrieve it as a Boolean (say true if its stored value != 0, false otherwise). Hibernate should use setFlag and getFlag for data persistency and retrieval, but isFlag is for app use alone; Hibernate does not and should not worry about it. Hibernate's behaviour is undefined in this case, when getFlag() and isFlag() both exist: the BasicPropertyAccessor will call Class.getDeclaredMethods() and choose the one of the above that comes first (http://cvs.sourceforge.net/viewcvs.py/hibernate/Hibernate3/src/org/hibernate/property/BasicPropertyAccessor.java?rev=1.8&view=markup): 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" /*boolean isBoolean = methods[i].getReturnType().equals(Boolean.class) || methods[i].getReturnType().equals(boolean.class);*/ 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; } API-Documentation for getDeclaredMethods() says "The elements in the array returned are not sorted and are not in any particular order", so you don't actually know which accessor will be chosen: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getDeclaredMethods() http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getDeclaredMethods() Some priority should be defined for this situation, for example choosing get() over is(). If possible, Hibernate should choose which one to use based on type as defined in the associated .hbm file. -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira |