Querying tables with compound primary keys using Spring -data-cassandra

I get the following exception when using the findOne () or findAll () method for 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)

My class definitions are as follows:

@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> {

}

Essentially, what happens here is that after reading a line from Cassandra while calling findOne () or findAll (), spring data cassandra tries to use the ClassGeneratingEntityInstantiator to instantiate an ItemAndLocation that does not know how to populate the composite primary key object, ItemAndLocationKey. I could not figure out how to create a custom instance of the object. There are no examples or documentation for the current version.

Any help would be greatly appreciated.

+4
2

, , @Table.

:

@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;
}
}

, :)

+2

, PrimaryKey. , , null Object s, false boolean 0 .

:

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

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

public, final, .

+2

Source: https://habr.com/ru/post/1620814/


All Articles