2

AdminController.java

    @Controller
    public class AdminController {

     @Autowired
        HttpServletRequest request;

        @Autowired
        AdminDao adminDao;

    @RequestMapping("/deletebatch")
        public String deletebatch(){
            int batchid = Integer.parseInt(request.getParameter("id"));
            adminDao.deletebatch(batchid);
            return "redirect:/viewbatch";
        }

#AdminDaoImpl.java

@Repository("adminDao")
public class AdminDaoImpl implements AdminDao {

    @Autowired
    SessionFactory sessionFactory;
@Transactional
     public void deletebatch(int batchid){
//         Batch batch = (Batch) sessionFactory.getCurrentSession().load(Batch.class,batchid);
//         if(batch!=null){
             sessionFactory.getCurrentSession().delete(batchid);
         //}

     }
}

#viewbatch.jsp

        <form >
            <table border="1">
                <tr>
                    <th>BATCH id</th>
                    <th>BATCH name</th>
                    <th>edit/delete</th>
                </tr>

                    <c:forEach items="${batchlist}" var="batchlist">
                        <tr>   
                        <td>${batchlist.batchid}</td>
                        <td>${batchlist.batchname}</td>
                        <td><a href="edit">edit</a>/<a href="${pageContext.servletContext.contextPath}/deletebatch?id=${batchlist.batchid}">delete</a></td>
                        </tr>


                    </c:forEach>

When i try to delete i got the error :

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.MappingException: Unknown entity: java.lang.Integer"

and i try putting the admincontroller as "/delete?id=${batchid}" also. While i did this i got the problem like can't convert to string

1 Answer 1

0

Session.delete(Object); takes the entity you want to delete as parameter, i.e. a Batch object in your example.

In getCurrentSession().delete(batchid); you're passing an Integer - Hibernate tries to delete the Entity Integer from the database but can't find a mapping and therefore throws a MappingException.

The code you commented out in AdminDaoImpl.java is actually what you need to get an entity!

Hibernate Session.delete() an object if exists has examples on how to delete entities in Hibernate. There are also some hints on whether you should use Session.get(Batch.class, batchid) instead of Session.load().

Sign up to request clarification or add additional context in comments.

5 Comments

as per your suggestion i put Batch batch = (Batch) session.get(Batch.class,batchid); and i got the problem 404 error
Your initial problem is in the ORM-Layer (Hibernate) - HTTP-Errors don't help here much as they are many layers above. Your HTTP 500 error contains the original java / Hibernate exception that actually helps. HTTP 404 is a "Page not found" and definitely another problem...
i know that 404 is a page not found error but when i change to that i got that problem but when i remove it, then it is as usual and work.
I'm pretty sure the 404 doesn't have anything to do with Hibernate; I'd assume that you have multiple problems in your code. Maybe you should try to get the different layers working independent of each other. Does the AdminController work, if you remove the adminDao.deletebatch(batchid);-call (see my last comment, this might be the reason for the 404). Does AdminDaoImpldeletebatch(int batchid) work when called directly from some test code (not in your web application)?
ya i got the solution of this problem and it is due to sessionFactory.getcurrentseesion().delete(new Batch(batchid)) and delete fucntion will take the object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.