I'm working on a Spring-MVC project, trying to update a user's username and password using jpa. But I don't seem to be making any progress. :D
I checked other questions like this one and tried to use their approved answers for my case. Alas, nothing changes in my db.
By far, I have tried these methods and none has worked:
using
persist
before set, as suggested hereusing
merge
after set as suggested herecommiting a transaction manually as suggested here.
Unfortunately, this class has a lot of foreign keys from all around my DB, and I can't safely remove the old one and add a new Dept
with new data in it. So, here goes my update method -in DAO level of course- would someone please suggest a new way to update username and password here?
@Transactional
public Dept updateDept(int depId, String u, String p) {
System.err.println("successfully reached DAO");
Dept dep=entityManager.find(Dept.class,depId);
dep.setUname(u);
dep.setPassword(p);
entityManager.merge(dep);
return entityManager.find(Dept.class,depId); //just to check if update really happened, which did not
}//end of update Dept
edit: Ok, so it is getting really complicated. I'm really a newbie in this area, so I try to give you as much detail as possible.
These are the only parts I was allowed to change, and I have to consider that any other configuration to other parts like JPA etc. is done by my senior team mate and is done correctly.
This is my controller which is supposed to update username and password of department manager, and send user back to their first page:
@RequestMapping(value="/infoEdit", method=RequestMethod.POST)
public String editManager(HttpServletRequest req, @RequestParam("username") String usernname , @RequestParam("password") String password , Model model) {
int id= (Integer) req.getSession().getAttribute("id");
boolean isUpdated=managerManagerImpl.updateUserPass(id,usernname,password);
System.err.println(isUpdated);
Dept d= managerManagerImpl.getDeptByManagerId(usernname,password);
if (d!=null)
return "themanager/managerFirstPage";
}
In managerManagerImpl - implementation of managerManager interface , updateUserPass()
goes like this:
@Override
public Boolean updateUserPass(int id, String usernname, String password) {
Dept dept=managerDAOImpl.getDept(id); // who is this?
Dept possibleDup=managerDAOImpl.getDept(usernname);//maybe this username is taken
Dept newDep=null;
if(possibleDup==null || (possibleDup!=null && possibleDup.getId()==dept.getId())){//username is not taken
System.err.println("going to update it!!");
newDep=managerDAOImpl.updateDept(dept.getId(),usernname,password);
System.err.println("newDep data here : " + newDep.getId() + "// "+newDep.getUname());
Dept newnewDept = managerDAOImpl.getDept(id);
if(newnewDept!=null)
System.err.println("&& newnewDep data here : " + newnewDept.getId() + "// "+newnewDept.getUname());
}
Boolean isUpdated=(null!=newDep && newDep.getId()==dept.getId());
return isUpdated;
}
and this is my whole managerDAOImpl class:
@Repository
public class TheManagerDAOImpl {
public TheManagerDAOImpl() {
}
@PersistenceContext
public EntityManager entityManager;
@Transactional
public Prof getManager(String usern){
String hql="SELECT p FROM Dept p WHERE p.uname=:username";
Query q= entityManager.createQuery(hql);
q.setParameter("username",usern);
List<Prof> res = (List<Prof>) q.getResultList();
return res == null || res.size() == 0 ? null : res.get(0);
}
@Transactional
public Dept getDept(int managerId) {
String hql= "SELECT d FROM Dept d WHERE d.id=:idHere";
Query q= entityManager.createQuery(hql);
q.setParameter("idHere",managerId);
List<Dept> res = (List<Dept>) q.getResultList();
return res == null || res.size() == 0 ? null : res.get(0);
}
@Transactional
public Dept getDept(String managerId, String managerPass) {
String hql= "SELECT d FROM Dept d WHERE d.uname=:username AND d.password=:pass";
Query q= entityManager.createQuery(hql);
q.setParameter("username",managerId);
q.setParameter("pass",managerPass);
List<Dept> res = (List<Dept>) q.getResultList();
return res == null || res.size() == 0 ? null : res.get(0);
}
@Transactional
public Dept getDept(String managerId) {
String hql= "SELECT d FROM Dept d WHERE d.uname=:username";
Query q= entityManager.createQuery(hql);
q.setParameter("username",managerId);
List<Dept> res = (List<Dept>) q.getResultList();
return res == null || res.size() == 0 ? null : res.get(0);
}
@Transactional
public Dept updateDept(int depId, String u, String p) {
System.err.println("**************************here in dao");
Dept dep=entityManager.find(Dept.class,depId);
dep.setUname(u);
dep.setPassword(p);
entityManager.merge(dep);
return entityManager.find(Dept.class,depId);
}//end of update Dept
}
Just to mention, those System.error stuff are used to make sure it actually calls these methods, and are completely irrelevant.