我希望创建如下模型,如何在 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




