How to map the row to the specific field in entity using R2DBC

151 views Asked by At

How to map the row from the table to single field.

Here is an example with book that holds the metadata in another class:

// Simple pojo that contains the information about papers
class PapersInfo {
    Long paperCount;
    Long blankPaperCount;
}
// Book entity
@Entity
class Book {
    @Id
    Long id;
    String name;
    PapersInfo paperInfo;

}

How to map the PapersInfo so it won't throw an exception?

So here is what I tried:

  • Using the AfterConvertCallback<Book> and then enhance property but it can't be done because of onAfterConvert(T entity, SqlIdentifier table) method does not contain any Row information.

  • Using the Converter from Spring core and use Converter<Row, Book> it works, but I need to map the whole entity by myself(including the book name, id and so on). In my project the entity is pretty large and mapping all the fields by myself is hard.

  • Using the Converter from Spring core but with Converter<Row, PapersInfo> but it is does not work, because MappingR2dbcConverter can parse Row only for entity class(for Book in this example). I thought about extending the MappingR2dbcConverter and override the read method, but almost all methods in MappingR2dbcConverter are private and I will need to rewrite it.

    Ideally I would like to override the

    private <R> R read(RelationalPersistentEntity<R> entity, Row row, @Nullable RowMetadata metadata)
    

    method from the MappingR2dbcConverter and add the ability to parse the Row to specific field, but it is private.

  • Of course I can annotate the PapersInfo with @Transient annotation and hold the fields from PapersInfo in Book class and map them in AfterConvertCallback but I think it should be better solution.

    Maybe I am missing something? Thanks for the help!

0

There are 0 answers