[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-1747) Issue choosing accessor method

Peter Thiemann (JIRA) noreply at atlassian.com
Fri Apr 27 03:36:04 EDT 2007


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-1747?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_26820 ] 

Peter Thiemann commented on HHH-1747:
-------------------------------------

JavaBeans-Spec says to prefer isFoo() over getFoo() for "boolean properties" - but they don't define that term....
If (like in Guilherme's example) isFoo() is just a convenience-method and there is a non-boolean setter don't we run into the danger of losing some updates when using isFoo()?
For example when the int Foo changes from 1 to 2 and isFoo() returns true in both cases?

So regardless of the Beans-specification I think getFoo() would be more adequate for internal Hibernate-use.

> Issue choosing accessor method
> ------------------------------
>
>                 Key: HHH-1747
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1747
>             Project: Hibernate3
>          Issue Type: Bug
>          Components: core
>    Affects 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

        



More information about the hibernate-issues mailing list