如何将Optional值绑定到Java中的SQL语句

时间:2019-01-14 10:56:52

标签: java serialization data-binding dao dropwizard

我正在使用dropwizard框架创建RESTful API。我似乎无法使用的方法是将Optional<Value>绑定到我的SQL语句中。

我也希望能够直接绑定一个对象,而不是一个个地传递参数,但是我找不到方法。这可能有相似或相同的解决方案。

    @SqlUpdate("INSERT INTO TASKS (project_id, creator_id, stage, title, description, created_at, priority) VALUES (:projectId, :creatorId, :stage, :title, :desc, :createdAt, :priority)")
    int insertTask(
            @Bind("projectId") int projectId,
            @Bind("creatorId") int creatorId,
            @Bind("stage") String stage,
            @Bind("title") String title,
            @Bind("desc") String description,
            @Bind("user") int userId,
            @Bind("createdAt") Timestamp createdAt,
            @Bind("priority") Optional<Priority> priority
    );

根据我的调试,这是错误所在。 priority:Optional[LOW]代替priority:"LOW"

UnableToCreateStatementException: Exception while binding 'priority'

[
statement: "UPDATE TASKS SET project_id = :projectId, stage = :stage, title = :title, description = :desc, priority = :priority WHERE task_id = :taskId",
located: "UPDATE TASKS SET project_id = :projectId, stage = :stage, title = :title, description = :desc, priority = :priority WHERE task_id = :taskId",
rewritten: "/* TasksDAO.updateTask */ UPDATE TASKS SET project_id = ?, stage = ?, title = ?, description = ?, priority = ? WHERE task_id = ?",
arguments: { positional:{}, named:{stage:'In Progress',title:'Test Task 1',priority:Optional[LOW],projectId:1,taskId:1,desc:'This is the first'},finder:[]}
]

0 个答案:

没有答案