Cassandra entities must have the @Table, @Persistent or @PrimaryKeyClass Annotation

无人久伴 提交于 2019-12-24 14:04:27

问题


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>{

} 

Configuration:

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:

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

}

Dependencies:

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

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.


回答1:


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

rather than:

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



回答2:


Your Inbound class does not know which table to refer in cassandra. Provide table name after @Table annotation like: @Table(value="table_name_here").



来源:https://stackoverflow.com/questions/33411059/cassandra-entities-must-have-the-table-persistent-or-primarykeyclass-annotat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!