ssh无法删除,大哥大姐帮帮忙吧!!!实在不知道了???
各位大哥大姐帮帮忙呀,本人新手,在网上搜索此类问题但都没能解决可以插入数据,但就是不能删除,当调用 noticeDao.delete(notice);时,出错
org.springframework.orm.hibernate3.HibernateSystemException: Provided id of the wrong type for class com.beans.notice.Notice. Expected: class java.lang.Integer, got class com.beans.notice.Notice; nested exception is org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.beans.notice.Notice. Expected: class java.lang.Integer, got class com.beans.notice.Notice
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:525)
org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:519)
com.dao.DaoSupport.get(DaoSupport.java:55)
com.dao.DaoSupport.delete(DaoSupport.java:47)
com.action.notice.NoticeAction.del(NoticeAction.java:71)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
当调用 noticeDao.delete(notice.getId());时,控制台没反应,也不报错,
http://localhost:8080/Shop/manager/notice/notice_del.action?id=18
package com.beans.notice;
import java.io.Serializable;
import java.util.Date;
public class Notice implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private Date createTime=new Date();
private String content;
private String managerNum;
public Notice(){
//构造函数
}
public Integer getId() {
return id;
}
public void setId(Integer newId) {
id = newId;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date newCreateTime) {
createTime = newCreateTime;
}
public String getContent() {
return content;
}
public void setContent(String newContent) {
content = newContent;
}
public String getManagerNum() {
return managerNum;
}
public void setManagerNum(String newManagerNum) {
managerNum = newManagerNum;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.beans.notice">
<class name="Notice" table="tb_notice">
<id name="id" >
<generator class="native"/>
</id>
<property name="content" />
<property name="createTime"/>
<property name="managerNum"/>
</class>
</hibernate-mapping>
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 指定连接数据库的驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3307/shop"/>
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root"/>
<!-- 指定连接数据库的密码 -->
<property name="password" value="java"/>
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="40"/>
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1"/>
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="1"/>
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20"/>
</bean>
<!-- 定义Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 依赖注入数据源,注入正是上面定义的dataSource -->
<property name="dataSource" ref="dataSource"/>
<!-- mappingResouces属性用来列出全部映射文件 -->
<property name="mappingResources">
<list>
<!-- 以下用来列出Hibernate映射文件 -->
<value>com/beans/notice/Notice.hbm.xml</value>
</list>
</property>
<!-- 定义Hibernate的SessionFactory的属性 -->
<property name="hibernateProperties">
<!-- 配置Hibernate属性 -->
<value>
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
<!-- 配置Hibernate的局部事务管理器,使用HibernateTransactionManager类 -->
<!-- 该类实现PlatformTransactionManager接口,是针对Hibernate的特定实现-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 配置HibernateTransactionManager时需要依注入SessionFactory的引用 -->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*add*" propagation="REQUIRED" />
<tx:method name="*save*" propagation="REQUIRED" />
<tx:method name="*del*" propagation="REQUIRED" />
<tx:method name="*update*" propagation="REQUIRED" />
<tx:method name="*modify*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- AOP配置的元素 -->
<aop:config>
<!-- 配置一个切入点,匹配org.crazyit.app.dao.impl包下
所有以Impl结尾的类里、所有方法的执行 -->
<aop:pointcut id="myPointcut"
expression="execution(* com.dao.*.*Imp.*(..))"/>
<!-- 指定在txAdvice切入点应用txAdvice事务增强处理 -->
<aop:advisor advice-ref="txAdvice"
pointcut-ref="myPointcut" />
</aop:config>
</beans>
public class NoticeAction extends ActionSupport implements ModelDriven<Notice>{
private static final long serialVersionUID = 1L;
private NoticeDao noticeDao;
private Notice notice=new Notice();
public String del() throws Exception{
notice = noticeDao.get(notice.getId());
noticeDao.delete(notice);
return list();
}
/*get set方法
*
*/
public Notice getNotice() {
return notice;
}
public void setNotice(Notice notice) {
this.notice = notice;
}
public NoticeDao getNoticeDao() {
return noticeDao;
}
public void setNoticeDao(NoticeDao noticeDao) {
this.noticeDao = noticeDao;
}
}
package com.dao.notice;
import com.dao.BaseDao;
import com.beans.notice.Notice;
public interface NoticeDao extends BaseDao<Notice> {
}
package com.dao.notice;
import com.beans.notice.Notice;
import com.dao.DaoSupport;
public class NoticeDaoImp extends DaoSupport<Notice> implements NoticeDao {
}
package com.dao;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.beans.PageModel;
//import com.lyq.dao.Transactional;
//import com.lyq.dao.Transactional;
import com.util.GenericsUtils;
public class DaoSupport<T> extends HibernateDaoSupport implements BaseDao<T>{
// 泛型的类型
@SuppressWarnings("unchecked")
protected Class<T> entityClass = GenericsUtils.getGenericType(this.getClass());
public void save(Object obj) {
getHibernateTemplate().save(obj);
}
public void saveOrUpdate(Object obj) {
getHibernateTemplate().saveOrUpdate(obj);
}
public void update(Object obj) {
getHibernateTemplate().update(obj);
}
public void delete(Object obj) {
getHibernateTemplate().delete(obj);
}
public void delete(Serializable entityId) {
getHibernateTemplate().delete(get(entityId));
}
@SuppressWarnings("unchecked")
public T get(Serializable entityId) {
return (T)getHibernateTemplate().get(this.entityClass, entityId);
}
--------------------编程问答-------------------- delete的应该是id
noticeDao.delete(notice);//这里是对象了,改为noticeDao.delete(notice.getId()) --------------------编程问答-------------------- 也不行呀,控制台不报错,也没有sql语句,数据库中更没有变化,怎么办呀,我是新手,在网上找了很多资料都没能解决问题,事物配置什么的???? --------------------编程问答-------------------- notice = noticeDao.get(notice.getId());
noticeDao.delete(notice);
真搞不明白你这两句意义何在? --------------------编程问答-------------------- Provided id of the wrong type for class com.beans.notice.Notice. Expected: class java.lang.Integer,貌似问题在这里吧,看看ID的类型 --------------------编程问答--------------------
是的,没什么意义,只是看一下get好不好用,结果是控制台中有select语句,但没有insert语句????? --------------------编程问答--------------------