I have a project in JavaEE, my task is to affect a professor to a form( a student placement form), I have an attribute called 'nb_sup' which is the number of student supervised by a selected professor, this number is incremented by one each time a professor is affected to a student. The problem is that my function sets the 'nub_sup' to one no matter how many times I affect the same professor.
Here is my code
public void SetNbSupPrfessor(Professor prof)
{
Query query = em.createQuery("update Professor u set nb_sup=:nb_sup WHERE u.id =:id");
query.setParameter("nb_sup", (1+ prof.getNb_sup()));
query.setParameter("id",prof.getId() );
query.executeUpdate();
}
It's safe to say that the professor inherits from the entity 'User' ,the 'nb_sup' is of type int and the 'id' is auto-increment.
I edited my code so this one worked
Professor prof = em.createQuery("select p from Professor p where p.id = :id",Professor.class)
.setParameter("id",id)
.getSingleResult();
prof.setNb_sup(prof.getNb_sup()+1);
em.merge(prof);