Datastax Cassandra - 使用对象映射器时的继承 [英] Datastax Cassandra - Inheritance when using object mapper

查看:402
本文介绍了Datastax Cassandra - 使用对象映射器时的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Cassandra来维持相当简单的POJO,但使用类层次结构(许多子类,一个超类)。我使用Java和Datastax驱动程序(和对象映射器)。



我的问题是对象映射器似乎不识别超类中注释的字段。 p>

我实现的结构是:

  @Table(keyspace =。 ..,name =...)
public class Subclass extends Superclass {
public double x;
public double y;
public double z;

....
}

public class Superclass {
@PartitionKey(0)
@Column(name =user_id )
public long userId;

@PartitionKey(1)
@Column(name =device_id)
public long deviceId;
}

然后尝试保存子类对象:

  public static void main(String [] args){
Subclass data = new Subclass();
data.deviceId = 123; $ b $ d data.userId = 1212;
data.x = 0;
data.y = 1;
data.z = 2;

集群集群;
会话会话;

...

映射器<子类> mapper = new MappingManager(session).mapper(Subclass.class);

mapper.save(data);
}

这会抛出:

  com.datastax.driver.core.exceptions.InvalidQueryException:缺少必需的PRIMARY KEY部分user_id 

有没有办法让它运行在这样的结构?一个解决方案是重新声明子类中的userId / deviceID字段,但这将是相当丑陋(很多重复的代码)。任何想法如何归档一个良好的结构或这种情况下的最佳实践?

解决方案

暂时不幸的是,你不能这个。您需要创建两个具有重复结构的单独类。



有一个票证

如果你想以一个共同的方式来处理这两个实现的对象, /datastax-oss.atlassian.net/browse/JAVA-541rel =nofollow> JAVA-541 添加支持,如果您想在驱动程序中看到此功能,请投票。 p>

I try using Cassandra for persisting quite simple POJOs, but using a class hierarchy (many subclasses, one superclass). I am using Java and the Datastax driver (and the object mapper).

My problem is that the Object Mapper doesn't seem to recognize fields annotated in superclasses.

The structure I implemented is:

@Table(keyspace = "...", name = "...")
public class Subclass extends Superclass {  
    public double x;
    public double y;
    public double z;

   ....
}

public class Superclass {   
    @PartitionKey(0)
    @Column(name = "user_id")
    public long userId;

    @PartitionKey(1)
    @Column(name = "device_id")
    public long deviceId;
}

I then try to save Subclass objects:

public static void main(String[] args) {
    Subclass data = new Subclass();
    data.deviceId = 123;
    data.userId = 1212;
    data.x = 0;
    data.y = 1;
    data.z = 2;

    Cluster cluster;
    Session session;

    ...

    Mapper<Subclass> mapper = new MappingManager(session).mapper(Subclass.class);

    mapper.save(data);
}

This throws:

 com.datastax.driver.core.exceptions.InvalidQueryException: Missing mandatory PRIMARY KEY part user_id

Is there any way to get it running in a structure like this? One solution was to redeclare the userId / deviceID field in the subclass, but this would be quite ugly (much repetitive code). Any ideas how to archive a good structure or best practices for a case like this?

解决方案

For the time being unfortunately you cannot do this. You would need to create two separate classes with a repetitive structure. You could create an interface with the shared fields if you want to address objects of both implementations in a common way.

There is a ticket JAVA-541 for adding support for this, please vote if you would like to see this capability in the driver.

这篇关于Datastax Cassandra - 使用对象映射器时的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆