在Cassandra

时间:2017-04-13 19:06:46

标签: java cassandra spring-data complextype spring-data-cassandra

我正在为春天的Cassandra设置DAO。

现在我有一个关于在对象中多次使用复合类的问题。

我有这个课程设置:

@Table(value = "settings")
public class Setting  {

    @PrimaryKey
    private User owner;

    @Column("key")
    private String key;

    @Column("value")
    private String value;

    @Column("updated_by")
    private User updatedBy;
}

班级用户:

@PrimaryKeyClass
public class User implements Serializable{

    @PrimaryKeyColumn(name = "userId", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    private String id;

    @PrimaryKeyColumn(name = "userIdType", ordinal = 1, type = PrimaryKeyType.PARTITIONED)
    private String idType;
}

所以我两次使用User类。一旦成为主键"所有者"并且一旦" updatedBy"。

在CassandraOperations类中使用它时,它可以作为主键使用,但不能再作为另一列。

它抱怨已经使用了列名userId。说得通。 那我怎么能做这个呢?

我可以使用UserDefined类型吗?

CREATE TYPE test_keyspace.user (
    id text,
    id_type text
);

但是我如何从java Annotations中做到这一点?

或者,我怎样才能重复使用同一个类呢? 考虑到Cassandra中相对简单的数据结构,我可以将User类扁平化为单个字符串,如idType.id

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

好的,发现它here,由denzal回答。

我的课程现在看起来像:

@Table("settings")
public class Setting  {

    @PrimaryKeyColumn(name = "owner", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    @CassandraType(type = DataType.Name.UDT, userTypeName = "user_type") 
    private User owner;

    @PrimaryKeyColumn(name = "key", ordinal = 1, type = PrimaryKeyType.CLUSTERED)
    @CassandraType(type = DataType.Name.TEXT) 
    private String key;

    @CassandraType(type = DataType.Name.TEXT) 
    private String value;

    @CassandraType(type = DataType.Name.UDT, userTypeName = "user_type") 
    private User lastUpdatedBy;
}

用户类:

@UserDefinedType("user_type") 
public class User implements Serializable{

    @CassandraType(type = DataType.Name.TEXT) 
    private String id;

    @CassandraType(type = DataType.Name.TEXT)
    private IdType idType;

}

很好地工作。

相关问题