Cassandra entities must have the @Table, @Persiste

2019-08-09 00:33发布

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.

2条回答
乱世女痞
2楼-- · 2019-08-09 01:03

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

查看更多
狗以群分
3楼-- · 2019-08-09 01:12

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
查看更多
登录 后发表回答