日期:2014-05-20  浏览次数:20735 次

利用spring中this.getHibernateTemplate().execute()进行批量删除不成功,各位帮忙解决下?
本人用struts 2.1+spring2.5+hibernate 3.进行项目开发,需要批量删除数据,本想利用bulkUpdate方法的但是这个方法进行批量删除时用“?”表示的参数个数必须确定,所以采用this.getHibernateTemplate().execute()调用HibernateCallback()方法进行批量删除,POJO代码、DAO代码,请各位大虾指点。
数据表映射POJO类Admin源码如下:
Java code

ackage per.ssh.dao.Admin;

/**
 * Admin entity. @author MyEclipse Persistence Tools
 */

public class Admin implements java.io.Serializable {

    // Fields

    private String adminUser;
    private String adminPwd;
    private Integer adminLevel;

    // Constructors

    /** default constructor */
    public Admin() {
    }

    /** full constructor */
    public Admin(String adminUser, String adminPwd, Integer adminLevel) {
        this.adminUser = adminUser;
        this.adminPwd = adminPwd;
        this.adminLevel = adminLevel;
    }

    // Property accessors

    public String getAdminUser() {
        return this.adminUser;
    }

    public void setAdminUser(String adminUser) {
        this.adminUser = adminUser;
    }

    public String getAdminPwd() {
        return this.adminPwd;
    }

    public void setAdminPwd(String adminPwd) {
        this.adminPwd = adminPwd;
    }

    public Integer getAdminLevel() {
        return this.adminLevel;
    }

    public void setAdminLevel(Integer adminLevel) {
        this.adminLevel = adminLevel;
    }

}


.hbm.xml文档如下:
XML code

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="per.ssh.dao.Admin.Admin" table="admin" schema="dbo" catalog="mpr">
        <id name="adminUser" type="java.lang.String">
            <column name="admin_user" length="50" />
            <generator class="assigned" />
        </id>
        <property name="adminPwd" type="java.lang.String">
            <column name="admin_pwd" length="50" not-null="true" />
        </property>
        <property name="adminLevel" type="java.lang.Integer">
            <column name="admin_level" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


DAO文件中的具体方法如下:
Java code

public void deleteByIds(final Object[] ids)throws DAOException{
        for(int i=0;i<ids.length;i++){
            System.out.println(ids[i]);
        }
        final String queryString="delete Admin where adminUser in (:ids)";
        try{
            this.getHibernateTemplate().execute(new HibernateCallback() {
                public Object doInHibernate(Session session) {
                    Query query = session.createQuery(queryString);
                    query.setParameterList("ids", ids);
                    return query.executeUpdate();
                }
            });
        }
        catch(Exception ex){
            throw new DAOException(ex);
        }
        /*for(int i=0;i<ids.length;i++){
            try{
                Admin admin=(Admin)this.getHibernateTemplate().get(Admin.class,ids[i]);
                if(admin!=null){
                    this.getHibernateTemplate().delete(admin);
                }
            }
            catch(Exception ex){
                throw new DAOException(ex);
            }
            
        }*/
    }