我确信这应该很容易,但我已经被困了好几个小时而没有理解这段代码中真正发生的事情:
private void loadUnits() {
units = new ArrayList<Unit>(); //units is a class global variable
families = new ArrayList<Family>(); //families is a class global variable
Statement st;
Statement stFields;
ResultSet rs;
ResultSet rsFields;
try {
//Load units info
st = cnnSrc.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = st.executeQuery(Queries.qUnits);
while (rs.next()) {
units.add(new Unit(rs));
}//END_WHILE
ConnectionProvider.close(rs, st);
//First load fields info
stFields = cnnSrc.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rsFields = stFields.executeQuery(Queries.qFields);
//Now load the Family information and create their instances
st = cnnSrc.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = st.executeQuery(Queries.qFamily);
while (rs.next()) {
families.add(new Family(rs, rsFields, units));
this.debugGlobalParams();
}//END_WHILE
ConnectionProvider.close(rsFields, stFields);
ConnectionProvider.close(rs, st);
} catch (SQLException ex) {
LOGGER.error(ex.getMessage(), ex);
}//END_TRYCATCH
}//END_METHOD
public void debugGlobalParams() {
int n = units.size();
int nu = 0;
String unitNames = "";
LOGGER.debug("Debugging " + n + " Units");
for (int i = 0; i < n; i++) {
LOGGER.debug(units.get(i).getParkUnitTitleAersa());
}
n = families.size();
LOGGER.debug("Debugging " + n + " Families");
Family f;
for (int i = 0; i < n; i++) {
f = families.get(i);
LOGGER.debug("FamilyId = " + f.getFamilyId() + "; FamilyTitle = " + f.getFamilyTitle() + "; Tabla = " + f.getTabla());
nu = f.getUnits().size();
for (int j = 0; j < nu; j++) {
unitNames = unitNames + f.getUnits().get(j).getParkUnitTitleAersa() + ",";
}
LOGGER.debug("Units included => " + unitNames);
}
}//END_METHOD
这个方法被调用一次从DB我的简单域模型,它应该包含一些Family描述和单元描述。每个单元应属于一个且仅属于一个系列,并且该关联在我创建新的Family实例的第二个while
期间完成。 Unit构造函数方法很简单,但是要了解Family构造函数中最相关的代码:
public Family(ResultSet rs, ResultSet rsFields, ArrayList<Unit> uns) {
try {
//Some local variables assignment...
units = new ArrayList<Unit>();
int n = uns.size();
for(int i=0; i<n; i++){
if(uns.get(i).getFamilyId() == this.FamilyId){
this.units.add(uns.get(i));
}
}//END_FOR
} catch (SQLException ex) {
LOGGER.error(ex.getMessage(), ex);
}//END_TRYCATCH
}//END_METHOD
现在,问题在于家庭和他们的正确单位之间的联系没有正确完成,当我打电话给this.debugGlobalParams()
时,我发现每个家庭不仅得到他们的单位而且还有以前的家庭。单位。
例如,如果我有f1,f2和f3族,并且它们每个都有三个单位u11,u12,u13,u21,u22,u23,u31,u32,u33(其中第一个数字表示它应该与之关联的族) ),我应该得到:
f1 -> u11, u12, u13
f2 -> u21, u22, u23
f3 -> u31, u32, u33
但我得到了这个:
f1 -> u11, u12, u13
f2 -> u11, u12, u13, u21, u22, u23
f3 -> u11, u12, u13, u21, u22, u23, u31, u32, u33
我确定问题与参考文献有关但我不明白为什么......有人有什么想法吗?
谢谢!
答案 0 :(得分:0)
问题是您的调试显示代码。您将一个系列中的所有单元名称放入unitNames,但不要重置此String。快速破解将重置字符串:
n = families.size();
LOGGER.debug("Debugging " + n + " Families");
Family f;
for (int i = 0; i < n; i++) {
unitNames = ""; // <----------- ADD THIS LINE
f = families.get(i);
LOGGER.debug("FamilyId = " + f.getFamilyId() + "; FamilyTitle = " + f.getFamilyTitle() + "; Tabla = " + f.getTabla());
nu = f.getUnits().size();
for (int j = 0; j < nu; j++) {
unitNames = unitNames + f.getUnits().get(j).getParkUnitTitleAersa() + ",";
}
LOGGER.debug("Units included => " + unitNames);
}
更好的解决方案是使用迭代器或Iterables,并在循环中声明变量:
for (Family f : families) {
LOGGER.debug("FamilyId = " + f.getFamilyId() + "; FamilyTitle = " + f.getFamilyTitle() + "; Tabla = " + f.getTabla());
String unitNames = "";
for (Unit unit : f.getUnits() {
unitNames = unitNames + unit.getParkUnitTitleAersa() + ",";
}
}