我不能使用repository.save(mydata)我正在寻找一个使用Spring + Cassandra的JOIN替代方案

时间:2016-12-14 07:35:09

标签: java spring spring-boot cassandra spring-data

我开始接触Cassandra,但我遇到了麻烦,因为我无法加入。 由于JOIN不能用CQL完成,我考虑寻找替代方法并在Java应用程序端加入它。

具体来说,我使用@OneToMany并尝试加入实体,但出现以下错误。

有什么好的解决方案吗?

■项目结构

Apache Cassandra的SpringBoot + Spring数据

版本:

  • Spring Boot ::(v1.3.5.RELEASE)
  • 弹簧数据卡桑德拉-1.3.5.RELEASE
  • cassandra 2.1.16

■错误日志

com.datastax.driver.core.exceptions.InvalidQueryException: Unknown identifier emp
at com.datastax.driver.core.Responses$Error.asException(Responses.java:102) ~[cassandra-driver-core-2.1.9.jar:na]
at com.datastax.driver.core.DefaultResultSetFuture.onSet(DefaultResultSetFuture.java:149) ~[cassandra-driver-core-2.1.9.jar:na]
at com.datastax.driver.core.RequestHandler.setFinalResult(RequestHandler.java:183) ~[cassandra-driver-core-2.1.9.jar:na]
at com.datastax.driver.core.RequestHandler.access$2300(RequestHandler.java:44) ~[cassandra-driver-core-2.1.9.jar:na]
at com.datastax.driver.core.RequestHandler$SpeculativeExecution.setFinalResult(RequestHandler.java:751) ~[cassandra-driver-core-2.1.9.jar:na]

■来源:控制器

@RequestMapping(value = "/DepartmentsCassandra/form", method = RequestMethod.POST)
@Transactional(readOnly=false)
public ModelAndView form(
        @RequestParam("department_id") int department_id,
        @RequestParam("department_name") String department_name,
        ModelAndView mav){
    Departments mydata = new Departments();
    mydata.setDepartment_id(department_id);
    mydata.setDepartment_name(department_name);
    repository.save(mydata);// ← Error occurred !!!
    return new ModelAndView("redirect:/DepartmentsCassandra");
}

■来源:实体:部门

package com.example.cassandra.entity;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;
@Table(value="departments")
public class Departments {

@PrimaryKeyColumn(name = "department_id",ordinal = 1,type = PrimaryKeyType.PARTITIONED)
private int department_id;

@Column(value = "department_name")
private String department_name;

public Departments(int department_id,String department_name){
    this.department_id = department_id;
    this.department_name = department_name;
}

@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name="department_id",insertable=false,updatable=false)
private List<Employees> emp = new ArrayList<Employees>();

1 个答案:

答案 0 :(得分:0)

Sooo,“客户端连接”是使用Cassandra的一般反模式,因为每次执行两次查询而不是一次,因此会失去性能提升。 要做的就是为每个查询创建一个宽泛的表 - 包括所有“已加入”的数据。 因此,在您的情况下,创建一个表employee_by_department或类似的东西。 查看datastax.com上的入门课程 - 它们很棒: - )