如何将Jdbi @GeneratedKeys与@RegisterMapper结合起来(how to combine Jdbi @GeneratedKeys with @RegisterMapper)

编程入门 行业动态 更新时间:2024-10-27 12:35:17
如何将Jdbi @GeneratedKeys与@RegisterMapper结合起来(how to combine Jdbi @GeneratedKeys with @RegisterMapper) @RegisterMapper(PhoneNumberMapper.class) public interface PhoneNumberJdbiDAO { @Transaction @GetGeneratedKeys(columnName = "ID", value = OracleGeneratedKeyMapper.class) @SqlUpdate("INSERT INTO PHONE_NUMBER (ID, COUNTRY_CODE_ISO2, PHONE_NUMBER, CREATE_DATETIME, UPDATE_DATETIME) " + "VALUES (PHONE_NUMBER_SEQ.NEXTVAL, :countryCode, :phoneNumber, :createDateTime, :updateDateTime)") PhoneNumber save(@BindBean PhoneNumber phoneNumber); }

OracleGeneratedKeyMapper.class和PhoneNumberMapper.class可以共存以返回映射的bean吗? 当我执行它时, java.lang.ClassCastException: java.math.BigDecimal cannot be cast to me.nave.persistence.domain.PhoneNumber为java.lang.ClassCastException: java.math.BigDecimal cannot be cast to me.nave.persistence.domain.PhoneNumber 。

目的是返回一个带有生成ID的bean。

PhoneNumberMapper

public class PhoneNumberMapper implements org.skife.jdbi.v2.tweak.ResultSetMapper<PhoneNumber> { @Override public PhoneNumber map(int index, ResultSet rs, StatementContext ctx) throws SQLException { CountryCode countryCodeIso2 = CountryCode.getByCodeIgnoreCase(rs.getString("COUNTRY_CODE_ISO2")); DateTime phoneNumberCreateDatetime = new DateTime(rs.getTimestamp("CREATE_DATETIME").getTime(), DateTimeZone.UTC); DateTime phoneNumberUpdateDatetime = new DateTime(rs.getTimestamp("UPDATE_DATETIME").getTime(), DateTimeZone.UTC); return new PhoneNumber .Builder(countryCodeIso2, rs.getString("PHONE_NUMBER")) .id(rs.getBigDecimal("ID")) .createDateTime(phoneNumberCreateDatetime) .updateDateTime(phoneNumberUpdateDatetime) .build(); } }

这是OracleGeneratedKeyMapper。

public class OracleGeneratedKeyMapper implements org.skife.jdbi.v2.tweak.ResultSetMapper<BigDecimal> { @Override public BigDecimal map(int index, ResultSet r, StatementContext ctx) throws SQLException { return r.getBigDecimal(1); } }

表和序列

CREATE SEQUENCE "PHONE_NUMBER_SEQ" MINVALUE 1 INCREMENT BY 1; CREATE TABLE "TEST"."PHONE_NUMBER" ( "ID" NUMBER NOT NULL, "COUNTRY_CODE_ISO2" VARCHAR2(2 CHAR) NOT NULL, "PHONE_NUMBER" VARCHAR2(32 CHAR) NOT NULL, "CREATE_DATETIME" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP(0) NOT NULL, "UPDATE_DATETIME" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP(0) NOT NULL, CONSTRAINT "PHONE_NUMBER_PK" PRIMARY KEY ("ID") ); @RegisterMapper(PhoneNumberMapper.class) public interface PhoneNumberJdbiDAO { @Transaction @GetGeneratedKeys(columnName = "ID", value = OracleGeneratedKeyMapper.class) @SqlUpdate("INSERT INTO PHONE_NUMBER (ID, COUNTRY_CODE_ISO2, PHONE_NUMBER, CREATE_DATETIME, UPDATE_DATETIME) " + "VALUES (PHONE_NUMBER_SEQ.NEXTVAL, :countryCode, :phoneNumber, :createDateTime, :updateDateTime)") PhoneNumber save(@BindBean PhoneNumber phoneNumber); }

Can the OracleGeneratedKeyMapper.class and PhoneNumberMapper.class co-exist to return the mapped bean? When I execute this, java.lang.ClassCastException: java.math.BigDecimal cannot be cast to me.nave.persistence.domain.PhoneNumber is being thrown.

The intent is to return a bean with the generated ID.

The PhoneNumberMapper

public class PhoneNumberMapper implements org.skife.jdbi.v2.tweak.ResultSetMapper<PhoneNumber> { @Override public PhoneNumber map(int index, ResultSet rs, StatementContext ctx) throws SQLException { CountryCode countryCodeIso2 = CountryCode.getByCodeIgnoreCase(rs.getString("COUNTRY_CODE_ISO2")); DateTime phoneNumberCreateDatetime = new DateTime(rs.getTimestamp("CREATE_DATETIME").getTime(), DateTimeZone.UTC); DateTime phoneNumberUpdateDatetime = new DateTime(rs.getTimestamp("UPDATE_DATETIME").getTime(), DateTimeZone.UTC); return new PhoneNumber .Builder(countryCodeIso2, rs.getString("PHONE_NUMBER")) .id(rs.getBigDecimal("ID")) .createDateTime(phoneNumberCreateDatetime) .updateDateTime(phoneNumberUpdateDatetime) .build(); } }

Here is the OracleGeneratedKeyMapper.

public class OracleGeneratedKeyMapper implements org.skife.jdbi.v2.tweak.ResultSetMapper<BigDecimal> { @Override public BigDecimal map(int index, ResultSet r, StatementContext ctx) throws SQLException { return r.getBigDecimal(1); } }

The table and Sequences

CREATE SEQUENCE "PHONE_NUMBER_SEQ" MINVALUE 1 INCREMENT BY 1; CREATE TABLE "TEST"."PHONE_NUMBER" ( "ID" NUMBER NOT NULL, "COUNTRY_CODE_ISO2" VARCHAR2(2 CHAR) NOT NULL, "PHONE_NUMBER" VARCHAR2(32 CHAR) NOT NULL, "CREATE_DATETIME" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP(0) NOT NULL, "UPDATE_DATETIME" TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP(0) NOT NULL, CONSTRAINT "PHONE_NUMBER_PK" PRIMARY KEY ("ID") );

最满意答案

获取插入的id时不使用@Mapper和@RegisterMapper。 如果要在插入后返回PhoneNumber对象(仅包含id),可以使用以下映射器。

public class PhoneNumberIdMapper implements org.skife.jdbi.v2.tweak.ResultSetMapper<BigDecimal> { @Override public BigDecimal map(int index, ResultSet r, StatementContext ctx) throws SQLException { return new PhoneNumber(r.getBigDecimal(1)); } }

但是如果你想获得整个PhoneNumber对象,我们需要手动完成。 我能想到两个选择,

public abstract PhoneNumber savePhoneNumber(PhoneNumber phoneNumber) { BigDecimal id = save(phoneNumber); phoneNumber.setId(id) }

或者再次从DB中读取它。

public abstract PhoneNumber savePhoneNumber(PhoneNumber phoneNumber) { BigDecimal id = save(phoneNumber); getPhoneNumber(id); // reading it from DB }

@Mapper and @RegisterMapper is not used while getting inserted id back. If you want PhoneNumber object (with id alone) to be returned after insert, You can use following mapper.

public class PhoneNumberIdMapper implements org.skife.jdbi.v2.tweak.ResultSetMapper<BigDecimal> { @Override public BigDecimal map(int index, ResultSet r, StatementContext ctx) throws SQLException { return new PhoneNumber(r.getBigDecimal(1)); } }

But if you are looking to get the whole PhoneNumber object, we need to do it manually. I could think of two options,

public abstract PhoneNumber savePhoneNumber(PhoneNumber phoneNumber) { BigDecimal id = save(phoneNumber); phoneNumber.setId(id) }

Or read it from DB again.

public abstract PhoneNumber savePhoneNumber(PhoneNumber phoneNumber) { BigDecimal id = save(phoneNumber); getPhoneNumber(id); // reading it from DB }

更多推荐

本文发布于:2023-07-07 09:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结合起来   如何将   Jdbi   GeneratedKeys   RegisterMapper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!