I have sceneario in which I have to check if an onject instance exist in entity manager. It works fine when the instance eexists but throws null pointer exception when it doesn’t. When it doesn’t I have to do another thing. so how can catch this state? I already tried try catch, but it doesn’t work.
Sorry when I was asking this question I was on the way and trying to type in a different device that I am used to. My code is:
AJPAController aJPAController;
AClass aClass = aJPAController.find((Integer.parseInt(request.getParameter("id")));
try{
if(aJpaController.contains(aClass)){
response.sendRedirect("gosomewhere.com");
}
}
catch (java.lang.NullPointerException R){
response.sendRedirect("gosomewhereelse.com");
}
I might have a logical mistake in trying first find and then contains method, but I couldn’t find a better solution.
Thanks in advance.
You can check if an entity is managed (within the persistence context) with help of the contains method of your EntityManager