Cassandra 实体必须具有@Table、@Persistent 或@PrimaryKeyClass 注解

Posted

技术标签:

【中文标题】Cassandra 实体必须具有@Table、@Persistent 或@PrimaryKeyClass 注解【英文标题】:Cassandra entities must have the @Table, @Persistent or @PrimaryKeyClass Annotation 【发布时间】:2016-01-29 09:46:49 【问题描述】:

我正在开发 Spring Boot 应用程序并尝试连接 Data Stax Cassandra。下面是我写的。

package com.sampleProj.dto;

import java.io.Serializable;
import java.sql.Blob;
import java.sql.Timestamp;

import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;

@Table
public class Inbound implements Serializable

@PrimaryKey
private int transactionalId;

@Column
private Timestamp received;

@Column
private String source;

@Column
private String service;

@Column
private Blob message;

public Inbound(int transactionalId, Timestamp received, String source,      String service, Blob message) 
    super();
    this.transactionalId = transactionalId;
    this.received = received;
    this.source = source;
    this.service = service;
    this.message = message;


public Inbound() 
    // TODO Auto-generated constructor stub


public int getTransactionalId() 
    return transactionalId;


public void setTransactionalId(int transactionalId) 
    this.transactionalId = transactionalId;


public Timestamp getReceived() 
    return received;


public void setReceived(Timestamp received) 
    this.received = received;


public String getSource() 
    return source;


public void setSource(String source) 
    this.source = source;


public String getService() 
    return service;


public void setService(String service) 
    this.service = service;


public Blob getMessage() 
    return message;


public void setMessage(Blob message) 
    this.message = message;




道:

package com.sampleProj.dao;

import org.springframework.data.cassandra.repository.CassandraRepository;

import com.sampleProj.dto.Inbound;

public interface TripDAO extends CassandraRepository<Inbound>

 

配置:

package com.sampleProj;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SampleProConfiguration 

public static void main(String[] args) 
    ApplicationContext ctx = SpringApplication.run(SampleProConfiguration .class,    args);

Cassandra 配置:

package com.sampleProj;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;

@Configuration
@EnableCassandraRepositories
public class CassandraConfiguration extends AbstractCassandraConfiguration

@Bean
@Override
public CassandraClusterFactoryBean cluster() 
    CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
    cluster.setContactPoints("localhost");
    cluster.setPort(9042);
    return cluster;


@Override
protected String getKeyspaceName() 
    return "mykeyspace";


@Bean
@Override
public CassandraMappingContext cassandraMapping() throws  ClassNotFoundException 
    return new BasicCassandraMappingContext();



依赖关系:

<dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-cassandra</artifactId>
    </dependency>

但我得到了Cassandra entities must have the @Table, @Persistent or @PrimaryKeyClass Annotation 的异常。请帮助我解决问题。提前致谢。

【问题讨论】:

Cassandra 中的表名是什么?尝试将其添加到 @Table 注释中。另外,您是否正确映射了主键。此外,您的主键属性是原始 int,请尝试将类型更改为 Integer。 可能和这个问题有同样的问题:***.com/questions/26385533/… 【参考方案1】:

在这个例子中不是这样,但是对于未来的读者来说。我在注释我的 pojo/table 类时收到了同样的错误:

@javax.persistence.Table

而不是:

@org.springframework.data.cassandra.mapping.Table

【讨论】:

【参考方案2】:

您的 Inbound 类不知道要在 cassandra 中引用哪个表。在 @Table 注释之后提供表名,例如: @Table(value="table_name_here")

【讨论】:

以上是关于Cassandra 实体必须具有@Table、@Persistent 或@PrimaryKeyClass 注解的主要内容,如果未能解决你的问题,请参考以下文章

@PrimaryKeyColumn 注释必须具有 PARTITIONED 类型,用于 scala Cassandra Spring Data 应用程序

JPA Annotations - 如何检索具有特定列值的所有实体

Cassandra写超时写

Spring Cassandra:在另一个键空间中的运行时创建模式

快速获取具有一对多关系的核心数据

如何使用 Pig 从 Cassandra 加载 CF/TABLE