在使用JDBI的@BindBean将值插入到Dropwizard中的Mysql数据库中时,我始终遇到以下异常。问题似乎是JDBI无法在bean中找到属性。我已经将问题分离到一个单独的项目中,但无法弄清楚这出错的地方。我会非常感谢你的一些建议。
<img src="link" style="vertical-align:right;" />
这是我的价值对象
org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: Unable to execute, no named parameter matches "title" and no positional param for place 0 (which is 1 in the JDBC 'start at 1' scheme) has been set. [statement:"INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, `create_date`, `teaser_image_url`, `teaser_image_color`) VALUES ( :title, :teaser, :extDescription, unix_timestamp(), :teaserImageUrl, :teaserImageColor)", located:"INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, `create_date`, `teaser_image_url`, `teaser_image_color`) VALUES ( :title, :teaser, :extDescription, unix_timestamp(), :teaserImageUrl, :teaserImageColor)", rewritten:"/* CarDAO.createCar2 */ INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, `create_date`, `teaser_image_url`, `teaser_image_color`) VALUES ( ?, ?, ?, unix_timestamp(), ?, ?)", arguments:{ positional:{}, named:{class:class com.javaeeeee.dwstart.db.Car}, finder:[]}]
at org.skife.jdbi.v2.ColonPrefixNamedParamStatementRewriter$MyRewrittenStatement.bind(ColonPrefixNamedParamStatementRewriter.java:158)
at org.skife.jdbi.v2.SQLStatement.internalExecute(SQLStatement.java:1318)
at org.skife.jdbi.v2.Update.executeAndReturnGeneratedKeys(Update.java:78)
at org.skife.jdbi.v2.sqlobject.UpdateHandler$1.value(UpdateHandler.java:51)
at org.skife.jdbi.v2.sqlobject.UpdateHandler.invoke(UpdateHandler.java:75)
at org.skife.jdbi.v2.sqlobject.SqlObject.invoke(SqlObject.java:175)
at org.skife.jdbi.v2.sqlobject.SqlObject$1.intercept(SqlObject.java:75)
at org.skife.jdbi.v2.sqlobject.CloseInternalDoNotUseThisClass$$EnhancerByCGLIB$$8f365cd7.createCar2(<generated>)
at com.javaeeeee.dwstart.db.TestDatabase.testCreatecar2(TestDatabase.java:60)
我创建了以下DAO以使用http://jdbi.org/sql_object_api_argument_binding/中描述的@BindBean功能 - 两种插入方法都失败。
public class Car {
public Long carId;
public String title;
public String teaser;
public String teaserImageUrl;
public String teaserImageColor;
public String extDescription;
public Long createDate;
}
这是我的表定义。
package com.javaeeeee.dwstart.db.dao;
import com.javaeeeee.dwstart.db.Car;
import org.skife.jdbi.v2.sqlobject.BindBean;
import org.skife.jdbi.v2.sqlobject.GetGeneratedKeys;
import org.skife.jdbi.v2.sqlobject.SqlUpdate;
public interface CarDAO {
@GetGeneratedKeys
@SqlUpdate("INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, " +
"`create_date`, `teaser_image_url`, `teaser_image_color`) " +
"VALUES ( :c.title, :c.teaser, :c.extDescription, unix_timestamp(), " +
":c.teaserImageUrl, :c.teaserImageColor)")
Long createCar(@BindBean("c") Car car);
@GetGeneratedKeys
@SqlUpdate("INSERT INTO `car_tbl`(`title`, `teaser`, `ext_description`, " +
"`create_date`, `teaser_image_url`, `teaser_image_color`) " +
"VALUES ( :title, :teaser, :extDescription, unix_timestamp(), " +
":teaserImageUrl, :teaserImageColor)")
Long createCar2(@BindBean Car car);
}
我正在使用以下JUnit集成测试来重新创建错误:
CREATE TABLE IF NOT EXISTS `cardb`.`car_tbl` (
`car_id` INT NOT NULL AUTO_INCREMENT COMMENT '',
`title` VARCHAR(255) NOT NULL COMMENT '',
`teaser` VARCHAR(2048) NULL COMMENT '',
`ext_description` VARCHAR(4096) NULL COMMENT '',
`create_date` BIGINT(20) NOT NULL DEFAULT 0 COMMENT '',
`teaser_image_url` VARCHAR(2048) NULL COMMENT '',
`teaser_image_color` VARCHAR(8) NULL COMMENT '',
PRIMARY KEY (`car_id`) COMMENT '',
INDEX `idx_create_date` USING BTREE (`create_date` ASC) COMMENT '')
ENGINE = InnoDB;
我正在使用Java 1.8.0_05,Dropwizard 0.9.2和MySQL 5.7.9。
答案 0 :(得分:10)
BindBean声称可以使用JavaBeans。 JavaBeans需要getter和setter。
请在您的Car类中添加getter和setter。