本文介绍了Cassandra实体必须具有@ Table,@ Persistent或@PrimaryKeyClass批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on spring boot application and trying to connect with Data Stax Cassandra. Below is the I have written.

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;
}


}

DAO:

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);
}

}

CassandraConfiguration:

CassandraConfiguration:

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实体必须具有@Table, @Persistent或@PrimaryKeyClass注释。请帮助我解决问题。

But I am getting the exception as Cassandra entities must have the @Table, @Persistent or @PrimaryKeyClass Annotation. Please help me in resolving the issue. Thanks in advance.

推荐答案

在此示例中不是这种情况,但对于将来的读者。 pojo / table类,其中:

Not the case in this example, but for future readers.. I received the same error when annotating my pojo/table class with:

@javax.persistence.Table

而不是:

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

这篇关于Cassandra实体必须具有@ Table,@ Persistent或@PrimaryKeyClass批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:35