Querying Tables with Composite Primary Keys using Spring-data-cassandra

删除回忆录丶 提交于 2019-12-05 09:52:18

I had the same problem, the problem was in the class that annotated with @Table.

In your scenario, you have to remove the overloaded constructor in the following class:

@Table(value = ItemAndLocation.tableName)
public class ItemAndLocation {

    @org.springframework.data.annotation.Transient
    public static final String tableName = "ItemAndLocation";

    @PrimaryKey
    private ItemAndLocationKey itemAndLocationKey;

    // \\\\\\\\\\\\\\\\\\\\ REMOVE THIS CONSTRUCTOR //////////////////
    // public ItemAndLocation(ItemAndLocationKey itemAndLocationKey) {
    //    this.itemAndLocationKey = itemAndLocationKey;
    // }
    // ////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


    public ItemAndLocationKey getItemAndLocationKey() {
        return itemAndLocationKey;
    }

    public void setItemAndLocationKey(ItemAndLocationKey itemAndLocationKey) {
        this.itemAndLocationKey = itemAndLocationKey;
   }
}

I hope this helps :)

I had the same problem both in the Main entity class as well as the PrimaryKey-class. What fixed it for me was to supply default constructors that take no parameters and initialize all fields to some default value, meaning null for Objects, false for booleans and 0 for all other primitive types.

In your case that would mean to add:

private ItemAndLocation() {
    this.itemAndLocationKey = null;
}

and

private ItemAndLocationKey() {
    this.itemId = null;
    this.locationId = null;
}

The constructors do not need to be public, the fields can be final, the framework will set the values via reflection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!