代码之家  ›  专栏  ›  技术社区  ›  ghovat

Dropwizard JDBI 3 ResultSetMapper忽略字段

  •  0
  • ghovat  · 技术社区  · 7 年前

    我有一个Pojo:

    private long id;
    
        @NotEmpty
        @JsonProperty("name")
        private String name;
    
        @NotEmpty
        @JsonProperty("id")
        private String tagUuid;
    
        @NotEmpty
        @JsonProperty("archived")
        private boolean archived;
    
        @NotEmpty
        @JsonProperty("creationDate")
        private DateTime creationDate;
    
        private Integer count;
    
        @JsonCreator
        public Tag() {
        }
    
        public Tag(long id, String tagUuid, String name, boolean archived, Timestamp creationDate, Integer count) {
            this.id = id;
            this.tagUuid = tagUuid;
            this.name = name;
            this.archived = archived;
            this.creationDate = new DateTime(creationDate);
            this.count = count;
        }
    

    这是我的结果集映射器:

    public class TagMapper implements ResultSetMapper<Tag> {
    
        @Override
        public Tag map(int index, ResultSet r, StatementContext ctx) throws SQLException {
            return new Tag(
                    r.getLong("id"),
                    r.getString("tag_uuid"),
                    r.getString("name"),
                    r.getBoolean("archived"),
                    r.getTimestamp("creation_date"),
                    r.getInt("count")
            );
        }
    }
    

    但如果我这样做,我会得到一个例外:org.skife.jdbi.2版。exceptions.ResultSetException:尝试遍历结果集时引发异常。我试图在没有其他参数的情况下创建一个附加的标记构造函数。

    @SqlQuery("SELECT t.id, t.tag_uuid as tag_uuid, t.name, t.archived, t.creation_date FROM tags t WHERE t.tag_uuid = :tag_uuid LIMIT 1")
        public Tag fetchTagByUuid(@Bind("tag_uuid") String tagUuid);
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Hitobat    7 年前

    您只需在查询SQL中返回额外的列。

    @SqlQuery("SELECT t.id, t.tag_uuid as tag_uuid, t.name, t.archived, " +
              "t.creation_date, 0 AS count FROM tags t " +
              "WHERE t.tag_uuid = :tag_uuid LIMIT 1")
    public Tag fetchTagByUuid(@Bind("tag_uuid") String tagUuid);
    
        2
  •  0
  •   Piyush Upadhyay    7 年前

    r.getString("tag_uuid") != null (用于字符串) 然后 tag_uuid = r.getString("tag_uuid")