我正在开发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;
}
}
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);
}
}
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
。请帮我解决这个问题。提前谢谢。
本例并非如此,但适用于未来的读者。。我在用注释我的pojo/table类时收到了同样的错误
@javax.persistence.Table
而不是:
@org.springframework.data.cassandra.mapping.Table
Inbound类不知道在cassandra中引用哪个表。在@Table
注释后提供表名,如:@Table(value="table_name_here")
。