简体   繁体   中英

How to mapping postgresql jsonb to java object r2dbc

I have an entity mapping with a table pc_customer in postgreSql. I have an issue when mapping an field jsonb from postgreSql to Object in java use R2DBC. Here is my code:

@EqualsAndHashCode(callSuper = false)
@Data
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Table("pc_customer")
public class Customer extends KunlunDomain {
    @Id
    private Long id;
    private String secret;
    private String code;

    @Column(value = "customer_authorities")
    CustomerAuthorities customerAuthorities;
    String zaloId;

    @Data
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    public static class CustomerAuthorities implements Serializable {
        boolean importParcel = false;
        boolean printParcelStamp = false;
        boolean draftServicesEnable = true;
    }
}

Here is my error received **Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [io.r2dbc.postgresql.codec.Json$JsonByteArrayInput] to type [* com.example.Customer$CustomerAuthorities]

Column customer_authorities is jsonb . So what can i do to mapping jsonb to a class in this case? Sorry for my English

Create BiFunctional Row Mapper for mapping DB row with java object [On Repository]

Example -

@Autowired
private DatabaseClient databaseClient;

@Autowired
private ObjectMapper objectMapper
public static final BiFunction<Row, RowMetadata, Customer> customerRowMapper = (row, rowMetadata) -> Customer.builder()
          // ...rest of the field
          // call your setter fun here 
          .customerAuthorities(objectMapper.readValue(row.get("customer_authorities", String.class), Customer.CustomerAuthorities.class))
          .build();

public Flux<Customer> getAllCustomer() {
  return databaseClient
            .sql("YOUR SQL QUERY")
            .map(customerRowMapper) // here will call the biFunctional row mapper
            .all();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM