各位大哥大姐帮帮忙呀,本人新手,在网上搜索此类问题但都没能解决
可以插入数据,但就是不能删除,当调用 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=18package 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);
}

解决方案 »

  1.   

    delete的应该是id
    noticeDao.delete(notice);//这里是对象了,改为noticeDao.delete(notice.getId())
      

  2.   

    也不行呀,控制台不报错,也没有sql语句,数据库中更没有变化,怎么办呀,我是新手,在网上找了很多资料都没能解决问题,事物配置什么的????
      

  3.   

    notice = noticeDao.get(notice.getId());noticeDao.delete(notice);
    真搞不明白你这两句意义何在?
      

  4.   

    Provided id of the wrong type for class com.beans.notice.Notice. Expected: class java.lang.Integer,貌似问题在这里吧,看看ID的类型
      

  5.   

    是的,没什么意义,只是看一下get好不好用,结果是控制台中有select语句,但没有insert语句?????
      

  6.   

    Quote: 引用 4 楼 fengspg 的回复:

    Provided id of the wrong type for class com.beans.notice.Notice. Expected: class java.lang.Integer,貌似问题在这里吧,看看ID的类型[/quo
    我用noticeDao.delete(notice.getId())就是控制台什么也没有,也不报错,数据库中也没变化,这会是什么错误呢????????
      

  7.   

    !hibernate里的配置文件模糊。
    类-表的关系没有配置、字段类型没有配置
    <id name="id" >
    <generator class="native"/>
    </id>
    这个id的类型,对应的列没有配置,其他的一样!一开始就是报的id有误啦!
      

  8.   

    需要配置字段类型么,好像是根据类可以自动判断的吧,
    <hibernate-mapping package="com.beans.notice">
     <class name="Notice" table="tb_notice">
     <id name="id" >
      

  9.   

    。。你没有看错误信息吗Expected: class java.lang.Integer
      

  10.   

    <id name="yhbh" type="java.lang.String">
                <column name="YHBH" length="20" />
            </id>例如
      

  11.   

    XML基本不会配置,只会偷懒直接用annotation
      

  12.   

    问题应该是在你的主键类型上。看看你的数据库中的主键的类型是什么。另外,现在annotaion这么好用,XML太麻烦了吧。还容易出错
      

  13.   

    你前台传的是个Integer吧,本来照理应该用
    public void delete(Serializable entityId) {
      T t=get(entityId);
      if(t!=null){
       getHibernateTemplate().delete(t);
      }
    }
    但是因为你是Integer,对应的却是Delete(Object obj)那个方法上去了,根本没到上面的方法里,所以你需要的是Notice,而传的是Integer,类型不同.就报错了.
      

  14.   

    delete删除的应该是个对象,而不是个ID,哪有那么高级?就凭一个id知道你要删的是什么东西
      

  15.   

    不是呀,我用的是模型驱动呀public  class NoticeAction extends ActionSupport implements ModelDriven<Notice>{
     private static final long serialVersionUID = 1L;
       
    private NoticeDao noticeDao; private Notice notice=new Notice(); 
      

  16.   


    当调用 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
      

  17.   

    那你在相应的方法上打个断点,看到时候用的是哪个方法,再看参数是什么.而且主要问题是,删除应该是持久状态的实体,你是ModelDriven过来的话,那时应该只是个临时状态的实体哈,不是持久态的不会删除的吧.
      

  18.   

    是不是你表结构的id类型不是number类型的
      

  19.   

    private Notice notice=new Notice(); 模型驱动是这样的?new Notice()应该放到getModel方法里面吧,你debug下你的notice对象是不是真的拿到了前台页面的数据啊。