日期:2014-05-19  浏览次数:20682 次

一个java中有趣的现象,大家讨论下是什么原因造成的呢?怎么解决呢?
大家先见如下代码:

public String findZGAndManigerByHandDepartment(String sheetHandDepartment){
System.out.println("传入的部门ID为:"+sheetHandDepartment);
HibernateTemplate ht = getHibernateTemplate();
final String str = sheetHandDepartment;
return (String) ht.execute(new HibernateCallback() {

public Object doInHibernate(Session session) throws
HibernateException {
Query queryRoot = session
.createSQLQuery("select {o.*} from TBL_DEPARTMENTS {o} where {o}.DEPARTMENT_ID='"+str+"'", "o", Department.class);
Iterator iter = queryRoot.list().iterator();
if (iter.hasNext()) {
Department ctvo = (Department) iter.next();
String sheetHand = ctvo.getParentID();
if (sheetHand.length()>1){
findZGAndManigerByHandDepartment(sheetHand);
}
System.out.println("返回的部门ID为:"+ctvo.getDeptID());
return ctvo.getDeptID();
}
return "";
}
});
}

调用该方法时传入参数为:findZGAndManigerByHandDepartment("8a74885a2f30559f012f8bb287430772");

你们假如这个递归只是被调用了三次,形如:
传入的部门ID为:8a74885a2f30559f012f8bb287430772
传入的部门ID为:8a74885a2f30559f012f8bb0e8dc074a
传入的部门ID为:ff8080812dc1758a012dc556ed0900ed
那么System.out.println("返回的部门ID为:"+ctvo.getDeptID());这句话会执行多少次呢?产生的原因是什么呢?怎么解决呢?


------解决方案--------------------
是九次吗?
------解决方案--------------------
不对,看循环几次,然后乘以三