使用 Spring-data-cassandra 查询具有复合主键的表 [英] Querying Tables with Composite Primary Keys using Spring-data-cassandra

查看:57
本文介绍了使用 Spring-data-cassandra 查询具有复合主键的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 CassandraReportory 的 findOne() 或 findAll() 方法时出现以下异常:

I'm getting the following exception when using the findOne() or findAll() method of CassandraReporitory:

Caused by: java.lang.IllegalStateException: property does not have a single column mapping
    at org.springframework.data.cassandra.mapping.BasicCassandraPersistentProperty.getColumnName(BasicCassandraPersistentProperty.java:134)
    at org.springframework.data.cassandra.convert.BasicCassandraRowValueProvider.getPropertyValue(BasicCassandraRowValueProvider.java:64)
    at org.springframework.data.cassandra.convert.BasicCassandraRowValueProvider.getPropertyValue(BasicCassandraRowValueProvider.java:35)
    at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:78)
    at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:71)
    at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:83)
    at org.springframework.data.cassandra.convert.MappingCassandraConverter.readEntityFromRow(MappingCassandraConverter.java:133)
    at org.springframework.data.cassandra.convert.MappingCassandraConverter.readRow(MappingCassandraConverter.java:115)
    at org.springframework.data.cassandra.convert.MappingCassandraConverter.read(MappingCassandraConverter.java:200)
    at org.springframework.data.cassandra.repository.query.AbstractCassandraQuery.getSingleEntity(AbstractCassandraQuery.java:176)
    at org.springframework.data.cassandra.repository.query.AbstractCassandraQuery.execute(AbstractCassandraQuery.java:133)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:454)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:432)

我的类定义如下:

@PrimaryKeyClass
public class ItemAndLocationKey implements Serializable {

    private static final long serialVersionUID = 1L;

    @PrimaryKeyColumn(name = "itemId", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    @CassandraType(type = Name.UUID)
    private UUID itemId;
    @PrimaryKeyColumn(name = "locationId", ordinal = 1, type = PrimaryKeyType.CLUSTERED)
    @CassandraType(type = Name.UUID)
    private UUID locationId;

    public ItemAndLocationKey(UUID itemId, UUID locationId) {
        this.itemId = itemId;
        this.locationId = locationId;
    }

    public UUID getItemId() {
        return itemId;
    }

    public void setItemId(UUID itemId) {
        this.itemId = itemId;
    }

    public UUID getLocationId() {
        return locationId;
    }

    public void setLocationId(UUID locationId) {
        this.locationId = locationId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ItemAndLocationKey that = (ItemAndLocationKey) o;

        if (!itemId.equals(that.itemId)) return false;
        return locationId.equals(that.locationId);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + itemId.hashCode();
        result = prime * result + locationId.hashCode();
        return result;
    }
}

<小时>

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

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

    @PrimaryKey
    private ItemAndLocationKey itemAndLocationKey;

    public ItemAndLocation(ItemAndLocationKey itemAndLocationKey) {
        this.itemAndLocationKey = itemAndLocationKey;
    }

    public ItemAndLocationKey getItemAndLocationKey() {
        return itemAndLocationKey;
    }

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

<小时>

public interface ItemAndLocationRepository extends TypedIdCassandraRepository<ItemAndLocation, ItemAndLocationKey> {

}

<小时>

本质上这里发生的事情是在调用 findOne() 或 findAll() 期间从 Cassandra 读取一行之后,spring data cassandra 尝试使用 ClassGeneratingEntityInstantiator 来实例化不知道如何填充复合主的 ItemAndLocation 对象关键对象,ItemAndLocationKey.我不知道如何提供自定义对象实例化器.当前版本没有示例或文档.


Essentially what is happening here is that after reading a row from Cassandra during the invocation of findOne() or findAll(), spring data cassandra attempts to use ClassGeneratingEntityInstantiator to instantiate the ItemAndLocation object that does not know how to populate the composite primary key object, ItemAndLocationKey. I couldn't figure out how to supply a custom object instantiator. There are no examples or documentation for the current release.

任何帮助将不胜感激.

推荐答案

我也遇到了同样的问题,问题出在用@Table注释的.

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 :)

这篇关于使用 Spring-data-cassandra 查询具有复合主键的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆