将Datastax实体映射器用于Cassandra时如何使用继承? [英] How do I use inheritance when using the Datastax entity mapper for Cassandra?

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

问题描述

我尝试使用Cassandra持久化非常简单的POJO,但是使用类层次结构(许多子类,一个超类)。我正在使用Java和Datastax驱动程序(以及对象映射器)。



我的问题是,对象映射器似乎无法识别超类中注释的字段。

p>

我实现的结构是:

  @Table(keyspace =。 ..,name = ...)
公共类子类扩展超类{
public double x;
公倍y;
public double z;

....
}

公共类超类{
@PartitionKey(0)
@Column(name = user_id )
公共长用户名;

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

然后我尝试保存子类对象:

  public static void main(String [] args){
子类数据=新的Subclass();
data.deviceId = 123;
data.userId = 1212;
data.x = 0;
data.y = 1;
data.z = 2;

群集群集;
会话会话;

...

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

mapper.save(data);
}

此抛出:

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

有什么办法可以使其在这样的结构中运行?一种解决方案是在子类中重新声明userId / deviceID字段,但这将非常难看(很多重复的代码)。有什么想法可以针对这种情况归档好的结构或最佳做法吗?

解决方案

更新:驱动程序现在支持继承,如3.1.0版中的问题中所述。请参见驱动程序的多态支持部分手册以获取更多详细信息:


在映射实体类或UDT类时,映射器将透明地扫描超类和超接口以查找在字段和getter方法上进行注释,从而使一个类层次结构可以多态映射到不同的CQL表或UDT中。


Java驱动程序4.1。 0+还支持实体和dao的继承。



原始答案:



很遗憾,您暂时无法执行此操作。您将需要创建两个具有重复结构的单独的类。如果您想以一种通用的方式处理两个实现的对象,则可以创建一个带有共享字段的接口。



有一个票证 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?

解决方案

Update: Inheritance is now supported by the driver as described in the question as of version 3.1.0. See Polymorphism Support section of the driver manual for more details:

When mapping an entity class or a UDT class, the mapper will transparently scan superclasses and superinterfaces for annotations on fields and getter methods, thus enabling the polymorphic mapping of one class hierarchy into different CQL tables or UDTs.

Java driver 4.1.0+ will also support inheritance for entities and daos.

Original Answer:

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天全站免登陆