我正在使用maven在spring MVC上做一个项目。我使用Ajax将我的数据传递给controller.it是好的..但是当我调用该函数进行删除时,我得到了org.hibernate.MappingException:未知实体:java .lang.Integer。低于我的代码..等待你的回复
的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>
弹簧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");
}
我的错误是当我从控制器调用函数delete
时答案 0 :(得分:4)
请注意hibernate方法getCurrentSession().delete(Object obj)
,而不只是将id
作为参数。
@Override
public void delete(int id) {
Account accounts = new Accounts();
// hibernate deletes objects by the primary key
accounts.setId(id);
getCurrentSession().delete(accounts);
}
答案 1 :(得分:1)
getCurrentSession()
方法将返回session
接口的实现。从3.6中查找javadocs并进一步查找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.
在accountsDAO.delete()
方法中,您将字符串id
传递给delete方法。
您必须获取要删除的Account
并传递Account
对象以进行删除。江提到的也会奏效。
答案 2 :(得分:0)
只需在AccountsDAO实施类
中添加并替换以下代码段即可
@Transactional
public void delete(int id) {
Account acc = new Account();
acc.setId(id);
Session session=sessionFactory.getCurrentSession();
session.delete(acc);
}
&#13;
答案 3 :(得分:0)
简单的解决方案:如果您使用Spring Framework,那么只需更改您的Dao&amp;服务层方法的参数类型。只需替换你的整数&#39;参数&#39;对象&#39;参数。例如:public void delete(int id)要替换&gt;&gt; public void delete(用户用户)。你的未知实体:java.lang.Integer&#39;错误将解决...