Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am doing a project on spring MVC with maven.I pass my data using Ajax to the controller.it is ok..but when i call the function for delete,i got org.hibernate.MappingException: Unknown entity: java.lang.Integer . below my codes..waiting for your reply

web.xml

            <?xml version="1.0" encoding="UTF-8"?>
            <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"            xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
            <servlet>
            <servlet-name>AccPerSpring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-context.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
            <servlet-name>AccPerSpring</servlet-name>
            <url-pattern>/</url-pattern>
            </servlet-mapping>
            </web-app>

spring-context.xml

            <?xml version="1.0" encoding="UTF-8"?>

            <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:beans="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"
            xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

             <!-- Enable @Controller annotation support -->
             <mvc:annotation-driven />

             <!-- Map simple view name such as "test" into /WEB-INF/views/test.jsp -->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
            </bean>

            <!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
            <context:component-scan base-package="com.gerrytan.pizzashop"/>

            <!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with 
   username root and blank password. Change below if it's not the case -->
            <!--  <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  -->
           <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
           <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
           <property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/>
           <property name="username" value="root"/>
           <property name="password" value="kca@fnpl#12"/>
      </bean>

        <!-- Hibernate Session Factory -->
        <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="packagesToScan">
        <array>
       <value>com.gerrytan.pizzashop</value>
        </array>
       </property>
       <property name="hibernateProperties">
       <value>
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
      </value>
      </property>
      </bean>

      <!-- Hibernate Transaction Manager -->
      <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="mySessionFactory"/>
      </bean>

      <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
      <!-- Activates annotation based transaction management -->
      <tx:annotation-driven transaction-manager="transactionManager"/>
      </beans>

Accountsconttroller .java

            package com.gerrytan.pizzashop;


            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.stereotype.Controller;
            import org.springframework.ui.ModelMap;
            import org.springframework.web.bind.annotation.RequestMapping;
            import org.springframework.web.bind.annotation.RequestMethod;
            import org.springframework.web.bind.annotation.RequestParam;
            import org.springframework.web.bind.annotation.ResponseBody;

            import com.google.gson.Gson;

            @Controller
            @RequestMapping(value="/account")
            public class AccountsController {
            @Autowired
             private AccountService accountService;
            @Autowired
            private AccountDAO accountDao;
            private Accounts accounts;

       @RequestMapping(value="/list",method = RequestMethod.GET,produces="application/json")
       @ResponseBody
       public String getAllAccounts(ModelMap model){

    model.addAttribute("count",accountDao.getRowCount());
    model.addAttribute("allAddress",accountDao.getAccounts());
    String json= new Gson().toJson(model);
    return json;
      }
             @RequestMapping(value="/delete",method=RequestMethod.POST,produces="application/json")
        @ResponseBody
        public ModelMap deleteRow(ModelMap model,@RequestParam(value = "id", required = true) int id) {

    System.out.println(id);
    accountDao.delete(id);
    model.addAttribute("messageKey", "1");
    model.addAttribute("id", id);
    return model;
}}

implimentation.java

    @Override
public void delete(int id) {

     getCurrentSession().delete(id);
     System.out.println("fgfgfgfgf");

}

my error is when iam calling the function delete from controller

share|improve this question
2  
You must pass the instance of the class to be deleted. In your case, if id represents the primary key of your table, get the object using this id and then delete the object. –  prasanth 13 hours ago
 
What version of Hibernate do you use? –  Hrishikesh 13 hours ago
 
hibernate 4.2.2 –  sneha 13 hours ago
add comment

2 Answers

up vote 1 down vote accepted

Notice the hibernate method getCurrentSession().delete(Object obj), not just give an id as parameter.

@Override
public void delete(int id) {
    Account accounts = new Accounts();
    // hibernate deletes objects by the primary key
    accounts.setId(id);
    getCurrentSession().delete(accounts);
}
share|improve this answer
 
you are right..i got it –  sneha 12 hours ago
add comment

The getCurrentSession() method would return an implementation of the session interface. Looking up the javadocs from 3.6 and further for Hibernate. It supports 2 methods for deletion

 void   delete(Object object) 
        Remove a persistent instance from the datastore.

 void   delete(String entityName, Object object) 
        Remove a persistent instance from the datastore.

In your accountsDAO.delete() method, you are passing in a String id to the delete method.

You would have to fetch the Account that you want to delete and pass the Account object for deletion. What Jiang mentioned would work too.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.