2

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

2
  • 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. Commented Mar 12, 2014 at 5:07
  • What version of Hibernate do you use? Commented Mar 12, 2014 at 5:19

4 Answers 4

5

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

Just add and replace with following code snippet in your AccountsDAO Implementation class

@Transactional 
public void delete(int id) {
    Account acc = new Account();
    acc.setId(id);
    Session  session=sessionFactory.getCurrentSession();
		session.delete(acc);
}

Comments

0

Simple solution : If you work with Spring Framework then just change in your Dao & Service layer method's parameter type. simply replace your 'Integer' parameter to 'Object' parameter. Eg : public void delete(int id) To replace >> public void delete(User user). your 'Unknown entity: java.lang.Integer' error will solve...

Comments

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.