liveBook · Manning

User-defined type

As explained in Spring in Action, Sixth Edition

A Cassandra user-defined type is a schema-defined composite type whose fields can be stored together in a column.

A Cassandra user-defined type (UDT) is a schema-defined composite value whose fields are stored together in a column. UDTs let collections hold richer structures than native values such as strings or integers. section 4.1.3

UDTs support Cassandra’s denormalized design. Instead of storing a foreign-key reference and joining another table, a column can contain copied data from the related record. For taco ingredients, the collection stores each ingredient’s name and type directly. section 4.1.3

When mapping a UDT with Spring Data Cassandra:

  • Annotate its class with @UserDefinedType, then define the fields that should be stored. section 4.1.3
  • Create a separate UDT class when the original class is already mapped with @Table; an entity cannot also serve as the UDT mapping. section 4.1.3
  • Include only the copied properties needed by the embedded value; an identifier is optional and usually unnecessary. section 4.1.3
package tacos;
 
import org.springframework.data.cassandra.core.mapping.UserDefinedType;
 
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
 
@Data
@RequiredArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@UserDefinedType("ingredient")
public class IngredientUDT {
  
  private final String name;
  
  private final Ingredient.Type type;
  
}

A minimal IngredientUDT maps ingredient name and type without an entity identifier. section 4.1.3

package tacos;
 
import java.util.List;
import org.springframework.data.cassandra.core.mapping.UserDefinedType;
import lombok.Data;
 
@Data
@UserDefinedType("taco")
public class TacoUDT {
 
  private final String name;
  private final List<IngredientUDT> ingredients;
 
}

A TacoUDT demonstrates nesting UDT values inside a list for a composite stored value. section 4.1.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains collection constraints, denormalization, annotation, separate classes, and optional fields without covering schema operations.
Examples
Uses taco ingredients, IngredientUDT, TacoUDT, and a stored ingredient collection as concrete mapping examples.
What it assumes
Assumes basic Cassandra tables and columns while teaching why native collection elements require UDTs for richer structures.
What you can do after
Build it from scratch by defining a separate annotated UDT class for structured values embedded in Cassandra columns.
How it connects
Connects user-defined types with Cassandra collections, denormalization, foreign keys, entities, and Spring Data Cassandra mappings.

Also explained in

Compare how 5 books explain user-defined type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.