Java Hibernate : Update most recent entry in MySql database
<p>This is my requirement. I have a bunch of rows with similar data. I want to update some columns on the LATEST entry. So I have written a hibernate query which goes like this</p>

<pre><code>String hql = "UPDATE Studenttable T set T.timestamp=:time,T.Action=:action where T.StudentId=:studentId and T.teacherId=:TeacherId order by T.teacher_student_mapping_id DESC";
Query query = session.createQuery(hql);
query.setParameter("time",time);
query.setParameter("studentId", studentId);
query.setParameter("TeacherId", teacherId);
query.setParameter("action", action);
query.setMaxResults(1);
query.executeUpdate();
</code></pre>

<p>What this does is it updates all the rows which satisfies the condition and then returns the latest row. Instead I want it to fetch the latest row satisfying the conditions, and then update it. How can I do it? Any help is deeply appreciated.</p>

<p>P.S. the teacher_student_mapping_id is an auto-generated value, which is also a primary key. </p>

<p>Time is the current time.</p>

<p>Please don't try to make sense of the table, I have changed the names of the columns for confidentiality.</p>