org.hibernate.MappingException:未知实体:java.lang.Integer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22342450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:03:48  来源:igfitidea点击:

org.hibernate.MappingException: Unknown entity: java.lang.Integer

javaspringhibernatemavenspring-mvc

提问by sneha

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

我正在用 maven 做一个关于 spring MVC 的项目。我使用 Ajax 将我的数据传递给控制器​​。没关系..但是当我调用删除函数时,我得到了 org.hibernate.MappingException: Unknown entity: java.lang。整数 。在我的代码下方..等待您的回复

web.xml

网页.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

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

帐户控制器 .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

实现.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

我的错误是当我从控制器调用函数删除时

采纳答案by Jiang

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

注意 hibernate 方法getCurrentSession().delete(Object obj),而不仅仅是给出一个idas 参数。

@Override
public void delete(int id) {
    Account accounts = new Accounts();
    // hibernate deletes objects by the primary key
    accounts.setId(id);
    getCurrentSession().delete(accounts);
}

回答by Hrishikesh

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

getCurrentSession()方法将返回session接口的实现。仰望的javadoc3.6,并进一步对Hibernate。它支持2种删除方法

 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 idto the delete method.

在您的accountsDAO.delete()方法中,您将 String 传递id给 delete 方法。

You would have to fetch the Accountthat you want to delete and pass the Accountobject for deletion. What Jiang mentioned would work too.

您必须获取Account要删除的Account对象并传递对象以进行删除。江说的也行。

回答by Abbey

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

只需在 AccountsDAO 实现类中添加和替换以下代码片段

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

回答by Ramkrishna Sitap

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...

简单的解决方案:如果您使用 Spring Framework,那么只需更改 Dao & Service 层方法的参数类型。只需将您的 'Integer' 参数替换为 'Object' 参数。例如:public void delete(int id) 替换>> public void delete(User user)。您的“未知实体:java.lang.Integer”错误将解决...