我正在寻找如下创建模型,如何在spring-data-cassandra中使用用户定义的类型?
{ email: "test@example.com", name: { fname: "First", lname: "Last" } }
Spring Data Cassandra现在支持用户定义的数据类型。最新版本1.5.0.RELEASE使用Cassandra Data stax驱动程序3.1.3,因此现在可以工作。请按照以下步骤操作
如何在Spring Data Cassandra中使用UserDefinedType(UDT)功能:
组:“ org.springframework.data”,名称:“ spring-data-cassandra”,版本:“ 1.5.0.RELEASE”
datastax.cassandra.driver.version = 3.1.3 spring.data.cassandra.version = 1.5.0.RELEASE spring.data.commons.version = 1.13.0.RELEASE spring.cql.version = 1.5.0.RELEASE
地址数据类型
CREATE TYPE address_type ( id text, address_type text, first_name text, phone text );
CREATE TABLE employee( employee_id uuid, employee_name text, address frozen, primary key (employee_id, employee_name) );
@Table("employee") public class Employee { -- othere fields-- @CassandraType(type = DataType.Name.UDT, userTypeName = "address_type") private Address address; }
@UserDefinedType("address_type") public class Address { @CassandraType(type = DataType.Name.TEXT) private String id; @CassandraType(type = DataType.Name.TEXT) private String address_type; }
@Bean public CassandraMappingContext mappingContext() throws Exception { BasicCassandraMappingContext mappingContext = new BasicCassandraMappingContext(); mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster().getObject(), cassandraKeyspace)); return mappingContext; }
用户定义的类型在任何地方都应具有相同的名称。例如
@UserDefinedType("address_type") @CassandraType(type = DataType.Name.UDT, userTypeName = "address_type") CREATE TYPE address_type