What’s new in Spring Data Lovelace for Redis and Apache Cassandra? | Java

0
Want create site? Find Free WordPress Themes and plugins.

Map and Tuple types are specific types in that allow storing multiple values within a single column. Previously, we supported both types in their raw form in mapped entities, which means that you could use only maps with primitive keys and values. For tuples, you could only use TupleValue, the raw Cassandra driver type, without further mapping or even schema support.

With this release, we added mapping and conversion support for Map- and Tuple-typed properties. Maps can now contain non-primitive keys and values, and the conversion layer applies potentially registered converters.

Consider the following types:

@UserDefinedType
class Manufacturer {
%MINIFYHTMLa358f62233f2a4693ffc223bae4e33e46%%MINIFYHTMLa358f62233f2a4693ffc223bae4e33e47%
String name; // getters/setters omitted }
@Table class Supplier { Map> acceptedCurrencies; // getters/setters omitted }

Manufacturer is a mapped user-defined type that is used by the map as a key. Values are represented as List of strings. We can now refactor the code to use a proper Currency type (such as .util.Currency) within the list. To do so, we provide converters between String and Currency and register these through CassandraCustomConversions. The following example shows how to do so:

enum StringToCurrencyConverter implements Converter {
  INSTANCE;

  @Override
  public Currency convert(String source) {
    return Currency.getInstance(source);
  }
}

enum CurrencyToStringConverter implements Converter {

  INSTANCE;

  @Override
  public String convert(Currency source) {
    return source.getCurrencyCode();
  }
}

@Configuration
class MyCassandraConfiguration {

  public CassandraCustomConversions cassandraCustomConversions() {
    return new CassandraCustomConversions(
      Arrays.asList(StringToCurrencyConverter.INSTANCE, CurrencyToStringConverter.INSTANCE));
  }
}

After registering the converters, we can go forward and use Currency within the Supplier type to work with value objects instead of primitives, as the following example shows:

@Table
class Supplier {

  Map> acceptedCurrencies;

  // getters/setters omitted
}

Tuples were not really usable with previous versions of Data for Cassandra. Using tuples required direct Row interaction and TupleType retrieval to create appropriate tuple values. Therefore, we decided to provide mapped tuple types, as the following example shows:

@Table
class Supplier {

  List dependances;

  // getters/setters omitted
}

@Tuple
class Dependance {

  @Element(0) String address;
  @Element(1) String city;
  @Element(2) Currency currency;

  // getters/setters omitted
}

Mapped tuples are annotated with @Tuple, and the individual components of a tuple refer (by using @Element(…)) to their ordinal index within the tuple. The converter inspects loaded tuples and maps these onto regular Java classes that are part of your domain model. You no longer need to interact with TupleType and TupleValue directly – although you still can – but you can represent tuple values in a type-safe approach. Mapped tuples benefit from the various mapping-features of the converter and can refer to types that have a custom converter registered.

Support for maps and tuples also includes schema generation to rapidly set up a schema by deriving the type from your domain model.

See our examples for mapped tuples for further details.

Did you find apk for android? You can find new Free Android Games and apps.

You might also like More from author

Leave A Reply

Your email address will not be published.