为什么我得到TransientDataAccessResourceException:PreparedStatementCallback;无效的论点?

时间:2014-03-07 21:57:51

标签: spring spring-mvc jdbc prepared-statement jdbctemplate

我正在用JUnit测试这个功能,我得到 TransientDataAccessResourceException:PreparedStatementCallback;参数值无效:java.io.NotSerializableException;

public boolean delete(long idTaskPriority) {
    String sql = "DELETE FROM a_taskPriority WHERE idTaskPriority=:idTaskPriority";

    MapSqlParameterSource params = 
        new MapSqlParameterSource("idTaskPriority", idTaskPriority);

    return jdbc.update(sql, params) == 1;
}

错误跟踪:

org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; 
SQL [DELETE FROM a_taskPriority WHERE idTaskPriority=:idTaskPriority]; 
Invalid argument value: java.io.NotSerializableException; 
nested exception is java.sql.SQLException: 
Invalid argument value: java.io.NotSerializableException

我已尝试过另一种params类型,但我或多或少有同样的错误

public boolean delete(long idTaskPriority) {

    String sql = "DELETE FROM a_taskPriority WHERE idTaskPriority=:idTaskPriority";

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("idTaskPriority", idTaskPriority);

    return jdbc.update(sql, params) == 1;
}

在这种情况下,我得到了错误跟踪

org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; 
SQL [DELETE FROM a_taskPriority WHERE idTaskPriority=:idTaskPriority]; 
Invalid argument value: java.lang.ArrayIndexOutOfBoundsException; 
nested exception is java.sql.SQLException: Invalid argument value: java.lang.ArrayIndexOutOfBoundsException

我传递了一个长= 1,尝试使用功能但它不起作用。


优先级

package com.javalabs.web.dao;

import java.util.Date;

import org.springframework.stereotype.Component;

@Component("priority")
public class Priority {

    private long idTaskPriority = 0;
    private long sortOrder = 0;
    private String aka = "";
    private String priority = "";
    private Date timestamp;

    public Priority() {
    }

    public Priority(long idTaskPriority) {
        this.idTaskPriority = idTaskPriority;
    }

    /**
     * @param priority
     */
    public Priority(String priority) {
        this.priority = priority;
    }

    /**
     * @param order
     * @param aka
     * @param priority
     */
    public Priority(long sortOrder, String aka, String priority) {
        super();
        this.sortOrder = sortOrder;
        this.aka = aka;
        this.priority = priority;
    }

    public void setIdTaskPriority(long idTaskPriority) {
        this.idTaskPriority = idTaskPriority;
    }

    public long getIdTaskPriority() {
        return idTaskPriority;
    }

    public long getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(long sortOrder) {
        this.sortOrder = sortOrder;
    }

    public String getAka() {
        return aka;
    }

    public void setAka(String aka) {
        this.aka = aka;
    }

    public String getPriority() {
        return priority;
    }

    public void setPriority(String priority) {
        this.priority = priority;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    @Override
    public String toString() {
        return "Priority [idTaskPriority=" + idTaskPriority + ", sortOrder=" + sortOrder + ", aka=" + aka
                + ", priority=" + priority + ", timestamp=" + timestamp + "]";
    }

}

PriorityDao课程

public class PriorityDao {

    private JdbcTemplate jdbc;

    @Autowired
    public void setDataSource(DataSource jdbc) {
        this.jdbc = new JdbcTemplate(jdbc);
    }

...
    public boolean delete(long idTaskPriority) {
        MapSqlParameterSource params = new MapSqlParameterSource(
                "idTaskPriority", idTaskPriority);

        String sql = "DELETE FROM a_taskPriority WHERE idTaskPriority=:idTaskPriority";

        return jdbc.update(sql, params) == 1;
    }
}

2 个答案:

答案 0 :(得分:3)

确保您使用NamedParameterJdbcTemplate进行update操作。

答案 1 :(得分:0)

我尝试过的最后一次尝试,没有参数,直接在sql中替换id。

public boolean delete(long idTaskPriority) {

    String sql = "DELETE FROM a_taskPriority WHERE idTaskPriority=" + idTaskPriority;

    return jdbc.update(sql) == 1;
}

这种方式并不像参数那么简单,而且有效。 我无法弄清楚参数,MapSqlParameterSource或HashMap有什么问题。