spring data cassandra 仅保存列表中的单个对象

spring data cassandra saving only a single object in the list

型号

    @Table(value = "bad_data")
    public class BadData {
        private String objectID;
        private String type;
        private String problems;
        private String owner;
        private String formattedID;
        private String project;

    @PrimaryKey
    private String train;
    private String status;

    /* Default constructor. */
    public BadData () {
        this.objectID = "";
        this.type = "";
        this.problems = "";
        this.owner = "";
        this.formattedID = "";
        this.project = "";
        this.train = "";
        this.status = "";
    }


    /* Getters and setters. */
    public void setObjectID (String objectID) {
        this.objectID = objectID;
    }

    public String getObjectID () {
        return this.objectID;
    }

    public void setType (String type) {
        this.type = type;
    }

    public String getType () {
        return this.type;
    }

    public void setProblems (String problems) {
        this.problems = problems;
    }

    public String getProblems () {
        return this.problems;
    }

    public void setOwner (String owner) {
        this.owner = owner;
    }

    public String getOwner () {
        return this.owner;
    }

    public void setFormattedID (String formattedID) {
        this.formattedID = formattedID;
    }

    public String getFormattedID () {
        return this.formattedID;
    }

    public void setProject (String project) {
        this.project = project;
    }

    public String getProject () {
        return this.project;
    }

    public void setTrain (String train) {
        this.train = train;
    }

    public String getTrain () {
        return this.train;
    }

    public void setStatus (String status) {
        this.status = status;
    }

    public String getStatus () {
        return this.status;
    }

}

存储库

@Autowired
public void save (CassandraOperations db) {
        BadData badData1 = new BadData();
        badData1.setTrain("train");
        badData1.setFormattedID("fid");
        badData1.addProblems("problem1");
        badData1.setObjectID("id");
        badData1.setOwner("lokesh");
        badData1.setType("story");
        badData1.setProject("om");
        badData1.setStatus("open");

        BadData badData2 = new BadData();
        badData2.setTrain("train");
        badData2.setFormattedID("fid");
        badData2.addProblems("problem2");
        badData2.setObjectID("id");
        badData2.setOwner("lokesh");
        badData2.setType("story");
        badData2.setProject("om");
        badData2.setStatus("open");

        BadData badData3 = new BadData();
        badData3.setTrain("train");
        badData3.setFormattedID("fid");
        badData3.addProblems("problem3");
        badData3.setObjectID("id");
        badData3.setOwner("lokesh");
        badData3.setType("story");
        badData3.setProject("om");
        badData3.setStatus("open");

        List<BadData> data = new ArrayList<>();
        data.add(badData1);
        data.add(badData3);
        data.add(badData2);

        db.insert(data);
    }

我正在尝试保存三个对象并将它们放入列表中。我只将一个对象 (badData3) 保存到数据库中。我更改了列表中这些对象的顺序。我注意到列表中间的哪个对象都被保存了。有人能猜出可能的错误是什么吗

您的 @PrimaryKey 是火车字段,您将每个对象中的此字段设置为相同的值:"train" 因此它们将相互覆盖。

是(krsyk 正确),您的 @PrimaryKey 字段 (train) 的值 ("train") 对于列表中的所有实体都是相同的。

此外,你应该看看 SD Cassandra[=38] 中的测试 class (CassandraDataOperationsTest) 中相应的测试用例 (insertBatchTest) =] 测试套件。

为了安心,我在测试的末尾添加了以下代码片段...

assertThat(template.count(Book.class), is(equalTo(80l)));

而且,测试按预期通过了。

注意,我使用的是最新的 Spring Data Cassandra 1.4.2.RELEASE.

另请注意,因为 Cassandra 中的每个 INSERTUPDATE 实际上是一个“UPSERT”(参见 here ),那么由于重复的主键值,您将有效地覆盖列表中的每个条目(实体)。