最後のObjectで配列を上書きしていますが、同じ場所のSystem.out.println()
がすべての項目を正しく表示する理由がわかりません。私は、学生の得点を含む表をランクで更新しようとしていますが、すべての生徒が配列の最後の項目を取得します。最後の項目で配列が上書きされています
public static void updateSubjectRank(String subject, String extype, int academicyear, int semester, int level) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(EduResults.class);
cr.add(Restrictions.eq("subject", subject));
cr.add(Restrictions.eq("extype", extype));
cr.add(Restrictions.eq("acedemicyear", academicyear));
cr.add(Restrictions.eq("semester", semester));
cr.add(Restrictions.eq("level", level));
ScrollableResults items = cr.scroll();
int count = 0;
double[] scores = getSubjectScores(subject, extype, academicyear, semester, level);//this gets all the scores in a double array
int[] ranks = Rank.getRank(scores);//this gives me the ranks of the scores above
while (items.next()) {
EduResults res = (EduResults) items.get(0);
for (int i : ranks) {
res.setSubjectRank(i + 1);//this updates the database with the last item in the array
System.out.println(i+1);///this prints the ranks properly
session.saveOrUpdate(res);
}
if (++count % 100 == 0) {
session.flush();
session.clear();
}
}
tx.commit();
} catch (Exception asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
}
私は同様の質問を見ましたが、これはシステムアウトが正しくプリントするので奇妙に見えます。私はここに何かを逃していますか?
setSubjectRankメソッドの実装をチェックしましたか? – AhmedAbdelaal
これは私のゲッターセッタークラスのものです – ErrorNotFoundException