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




