用于spring-data-cassandra 2.0M1的CassandraRepository中的批量操作

时间:2017-02-03 11:00:12

标签: spring-data spring-data-cassandra

我有一个简单的spring-data-cassandra项目,试图使用

插入多个实体
<S extends T> Iterable<S> save(Iterable<S> entities)
CassandraRepository类的

但是,当我使用版本2.0.0.M1(适用于以前的版本)时,我收到以下错误,

Exception in thread "main" org.springframework.data.cassandra.mapping.VerifierMappingExceptions: java.util.ArrayList:
- Cassandra entities must be annotated with either @Persistent, @Table, @UserDefinedType or @PrimaryKeyClass

at org.springframework.data.cassandra.mapping.CompositeCassandraPersistentEntityMetadataVerifier$PersistentAnnotationVerifier.verify(CompositeCassandraPersistentEntityMetadataVerifier.java:92)
at org.springframework.data.cassandra.mapping.CompositeCassandraPersistentEntityMetadataVerifier.verify(CompositeCassandraPersistentEntityMetadataVerifier.java:70)
at org.springframework.data.cassandra.mapping.BasicCassandraPersistentEntity.verify(BasicCassandraPersistentEntity.java:160)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:332)
at org.springframework.data.cassandra.mapping.BasicCassandraMappingContext.addPersistentEntity(BasicCassandraMappingContext.java:381)
at org.springframework.data.cassandra.mapping.BasicCassandraMappingContext.addPersistentEntity(BasicCassandraMappingContext.java:65)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:185)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:145)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:70)
at org.springframework.data.cassandra.core.CassandraTemplate.getPersistentEntity(CassandraTemplate.java:427)
at org.springframework.data.cassandra.core.CassandraTemplate.getTableName(CassandraTemplate.java:443)
at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:314)
at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:302)
at org.springframework.data.cassandra.repository.support.SimpleCassandraRepository.save(SimpleCassandraRepository.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.executeMethodOn(RepositoryFactorySupport.java:553)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:538)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:479)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy34.save(Unknown Source)

我的主要班级App.java

public class App
{

public static void main(String[] args) {
    ApplicationContext context =new AnnotationConfigApplicationContext(CassandraConfig.class);
    CustomerRepo repo=context.getBean(CustomerRepo.class);
    List<Customer> customers=new ArrayList<Customer>();

    Customer cust=new Customer();
    cust.SetId("142");
    cust.setName("Mayor");
    cust.setAcc_type("new");
    cust.setAcc_name("savings");
    cust.setSegment("Normal");
    customers.add(cust);

    Customer cust1 = new Customer();
    cust1.SetId("143");
    cust1.setName("Final");
    cust1.setAcc_type("new");
    cust1.setAcc_name("savings");
    cust1.setSegment("Normal");
    customers.add(cust1);

    repo.save(customers);

   }
}

实体类Customer.java

@Table(value="Customer")
public class Customer {

 @PrimaryKeyColumn(name = "id",ordinal = 1,type = PrimaryKeyType.PARTITIONED)
 private String id;
 @Column(value ="name")
 private String name;
 @Column(value = "acc_name")
 private String acc_name;
 @Column(value = "acc_type")
 private String acc_type;
 @Column(value = "segment")
 private String segment;



 public Customer(String id, String name, String acc_name, String acc_type,
        String segment) {

     this.id=id;
     this.name=name;
     this.acc_name=acc_name;
     this.acc_type=acc_type;
     this.segment=segment;

}


public Customer() {

}


public void SetId(String id)
 {
     this.id=id;
 }


 public void setName(String name)
 {
     this.name=name;
 }


 public void setAcc_name(String acc_name)
 {
     this.acc_name=acc_name;
 }

 public void setAcc_type(String acc_type)
 {
     this.acc_type=acc_type;

 }

 public void setSegment(String segment)
 {
     this.segment=segment;

 }

 public String getId() {
  return id;
 }


 public String getName() {
  return name;
 }

 public String getAcc_name() {
  return acc_name;
 }

 public String getAcc_type() {
      return acc_type;
}

 public String getSegment() {
      return segment;
}


}

最后,存储库CustomerRepo.java

import com.Entity.Customer;
public interface CustomerRepo extends CassandraRepository<Customer> {

}

这是一个问题(我还没能通过Goolge或网站找到),还是我错过了一些注释?

1 个答案:

答案 0 :(得分:0)

对于批量查询,使用CassandraTemplate有助于插入具有多个实体的批量操作。这可以在Spring Data Cassandra中找到。

示例代码:

CassandraBatchOperations batchOps = cassandraTemplate.batchOps();  
batchOps(movieByGenre);  
batchOps(movieByActor);  
batchOps.insert(movie);  
batchOps.execute();  

这将在内部使用Cassandra本地批处理操作。