2
votes

After listening to a nice talk about Spring Data JDBC and Doamin-Driven Design by Jens Schauder (as an AFOL I love your examples;-) I experimented a bit but get stuck quite soon with my domain types. Of course I need to write custom JDBC converters, for example to map a TelephoneNumber to String and back, but how do I handle more complex types like a period which has a LocalDate as start date and a LocalDate as end date. Or an address with street, house no, zip code, city...

I can not add annotations to these types, because the can have a different meaning depending on their use, for example an invoice address vs. a delivery address.

Using JPA/Hibernate a can use @Columns or @AttributeOverrides or implement a compound hibernate user type, but in Spring Data JDBC a found only simple converters.

Do I missed something or is this not (yet) possible with Spring Data JDBC?

1

1 Answers

1
votes

I think what you are looking for is the @Embedded annotation. It is available in the 1.1.x versions. Using that (and Lombok to keep the code short) you can model a Period like this:

@Value
class Period {
    LocalDate from;
    LocalDate until;
}

And use it like this:

@Value
class SpecialOffer {
     String name;
     int rebate;

     @Embedded(onEmpty = OnEmpty.USE_EMPTY, prefix="valid_")
     Period valid;
}

This will map the object to a single table SPECIAL_OFFER with the columns NAME, REBATE, VALID_FROM, VALID_UNTIL

There is currently no way to specify a Converter for a single attribute only. The way to work around that would be to create a wrapper around the value and create a converter for that.